Rate This Document
Findability
Accuracy
Completeness
Readability

Setting JVM Heap Size

Principles

When executing a Java program, the JVM divides the memory managed by the JVM into several different runtime data areas, including a program counter, a method area, a VM stack, a local method stack, and a heap.

  1. A program counter can be regarded as a row number indicator of the bytecode being executed by the current thread.
  2. The method area is used to store data such as class information, constants, and static variables that are loaded by the JVM.
  3. The VM stack stores thread memory model for Java method execution. Each process from method call to execution completion corresponds to a stack frame in the VM stack from stack-in to stack-out.
  4. The function of the local method stack is the same as that of the virtual machine stack. The difference is that the local method stack only calls services for local methods.
  5. The heap is the largest part of the JVM management memory and is used to store Java program object instances. Almost all object instance memory is allocated here. From the perspective of memory reclamation and classical generation-based garbage collector, the heap memory space is generally divided into the following areas: young generation, old generation, permanent generation, Eden space, and from/to survivor.

Heap space optimization is one of the focuses of Java performance tuning. If the JVM heap size is not set, the JVM sets the default heap size based on the physical memory size of the server. For example, on a 64-bit server, when the physical memory is less than 192 MB, the JVM heap size is set to half of the physical memory by default. When the physical memory size is greater than 192 MB and less than 128 GB, the JVM heap size is a quarter of the physical memory size by default. If the physical memory size is greater than or equal to 128 GB, the value is 32 GB. Generally, the Java application specifies the heap size through parameters. For details, see the following sections.

The size and proportion of the heap space specified by an application are generally evaluated based on the GC status of the application and the memory resources of the server. This is a process of gradual optimization. If GC is frequently triggered, try to increase the heap space to alleviate the problem.

Recommended configuration policies:

  1. When the application is running, calculate the size (X) of the space occupied by the old-generation live objects. Set the heap size (Xmx and Xms) of the program to three to four times of X. Set PermSize and MaxPermSize to 1.2 to 1.5 times of X. Set Xmn of the young generation to 1 to 1.5 times of X. Set the size of the old-generation memory to 2 to 3 times of X.
  2. JDK officially recommends that the young generation occupy about 3/8 of the heap size.
  3. After a full GC, 70% of the heap space should be released (30% of the heap space is still occupied).

Modification Method

When the Java application is started, add the following parameters and set the sizes:

Parameter

Description

-Xmx

Maximum available heap memory of the JVM.

-Xms

Initial heap size, which is generally the same as that of Xmx.

-Xmn

Size of the young generation heap.

-Xss

Heap size for each thread. In JDK 1.5 and later versions, the default stack size of each thread is 1 MB. In earlier versions, the default stack size is 256 KB.

-XX:NewRatio=

Ratio of the young generation (including Eden and two survivor areas) to the old generation (excluding the permanent generation). For example, if this parameter is set to 4, the ratio of the young generation to the old generation is 1:4. That is, the young generation occupies 1/5 of the entire stack.

-XX:SurvivorRatio=

Ratio of the size of the Eden area to the size of the survivor area in the young generation. For example, if this parameter is set to 4, the ratio of two survivor areas to one Eden area is 2:4, and one Survivor area accounts for 1/6 of the entire young generation.

-XX:MaxPermSize=

Size of the permanent generation heap.

Example:

1
java -Xmx3600m -Xms3600m -Xmn2g -Xss128k

Set the maximum heap size to 3600 MB, the initial heap size to 3600 MB, the young generation size to 2 GB, and the thread heap size to 128 KB.