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
However, above algorithms are just for reference, it may vary in different VM.
1. 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 popularjava.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
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.
2. Ubuntu
This is the testing environment :
OS : Ubuntu 13 (64 bits) (Under VirtualBox)
RAM : 4G
CPU : 1 x Processors
JDK : 1.7.0_51
$ java -XX:+PrintFlagsFinal -version | grep -iE 'HeapSize|PermSize|ThreadStackSize'
uintx InitialHeapSize := 64781184 {product}
uintx MaxHeapSize := 1038090240 {product}
uintx PermSize = 21757952 {pd product}
uintx MaxPermSize = 174063616 {pd product}
intx ThreadStackSize = 1024 {pd product}
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.13.10.1)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
In above environment, JVM allocated following default values :
-
Java heap size
InitialHeapSize = 64781184 bytes (61.7M) and MaxHeapSize = 1038090240 bytes (990M). -
PermGen Size
PermSize = 21757952 bytes (20.75M), MaxPermSize = 174063616 bytes (166M) -
Thread Stack Size
ThreadStackSize = 1024 kilobytes (1M)
The allocated heap memory size is quite close to the ergonomics result.
#ergonomics algorithm
Initial heap size = 4096M/64 = 64M
Maximum heap size = 4096M/4 = 1024M