Tuesday 5 August 2014

Find Out Your Java Heap Memory Size

In this article, we will show you how to use the -XX:+PrintFlagsFinal to find out your heap size detail. In Java, the default and maximum heap size are allocated based on this – ergonomics algorithm.


Heap sizes
Initial heap size of 1/64 of physical memory up to 1Gbyte
Maximum heap size of 1/4 of physical memory up to 1Gbyte

Java Memory Overview

A quick review of Java memory structure :

1. Java Heap Size
Place to store objects created by your Java application, this is where Garbage Collection takes place, the memory used by your Java application.For a heavy Java process, insufficient Heap size will cause the popular java.lang.OutOfMemoryError: Java heap space.
-Xms<size> - Set initial Java heap size
-Xmx<size> - Set maximum Java heap size

$ java -Xms512m -Xmx1024m JavaApp


2. Perm Gen Size
Place to store your loaded class definition and metadata. If a large code-base project is loaded, the insufficient Perm Gen size will cause the popular Java.Lang.OutOfMemoryError: PermGen.
-XX:PermSize<size> - Set initial PermGen Size.
-XX:MaxPermSize<size> - Set the maximum PermGen Size.
 
$ java -XX:PermSize=64m -XX:MaxPermSize=128m JavaApp
 
 
3. Java Stack Size
Size of a Java thread. If a project has a lot of threads processing, try reduce this stack size to avoid running out of memory.
-Xss = set java thread stack size

$ java -Xss512k JavaApp

 
 
 Note
The default value for heap size, perm gen, or stack size is differ from different JVMs. The best practice is always defining your own value

Windows

There is no grep in Windows, instead, we use findstr.
This is the testing environment :
OS  : Windows 8
RAM : 16G
CPU : 8 x Processors
JDK : 1.7.0_40
C:\>java -XX:+PrintFlagsFinal -version | findstr /i "HeapSize PermSize ThreadStackSize"
 
    uintx InitialHeapSize                          := 266634176       
    uintx MaxHeapSize                              := 4267704320      
    uintx PermSize                                  = 21757952        
    uintx MaxPermSize                               = 85983232        
     intx ThreadStackSize                           = 0               

java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)
  1. Java heap size
    InitialHeapSize = 266634176 bytes (256M) and MaxHeapSize = 4266146816 bytes (4068M).
  2. PermGen Size
    PermSize = 21757952 bytes (20.75M), MaxPermSize = 85983232 bytes (823. M).
  3. Java Stack Size
    ThreadStackSize = 0 kilobytes. (weird…)
The allocated heap memory size is almost same with the ergonomics result :
#ergonomics algorithm
Initial heap size = 16384/64 = 256M
Maximum heap size = 16384/4 = 4096M
 
 

Suggested Java Memory

Below is my suggested value for a small to medium Java application :)
  1. Heap = -Xms512m -Xmx1024m
  2. PermGen = -XX:PermSize=64m -XX:MaxPermSize=128m
  3. Thread = -Xss512k
P.S For most Java projects, 512k for a thread is sufficient.
$ java -XX:+PrintFlagsFinal -Xms512m -Xmx1024m -Xss512k -XX:PermSize=64m -XX:MaxPermSize=128m
 -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
 
    uintx InitialHeapSize                          := 536870912       {product}
    uintx MaxHeapSize                              := 1073741824      {product}
    uintx PermSize                                 := 67108864        {pd product}  
    uintx MaxPermSize                              := 134217728       {pd product}  
    intx ThreadStackSize                           := 512             {pd product}
 
 
 

No comments:

Post a Comment