All Products
Search
Document Center

Serverless App Engine:JVM options

Last Updated:Jan 18, 2024

This topic describes the Java virtual machine (JVM) startup options that are used to configure the heap size and garbage collectors.

Configure the heap size

Common JVM options that are used to configure the heap size

Option

Description

Example

-Xmx

Sets the maximum heap size.

-Xmx3550m sets the maximum heap size to 3,550 MB.

-Xms

Sets the minimum heap size.

-Xms3550m sets the minimum heap size to 3,550 MB. To prevent reallocation of the heap size after each garbage collection, we recommend that you set the -Xms and -Xmx options to the same value.

-Xmn

Sets the size of the young generation.

-Xmn2g sets the size of the young generation to 2 GB. Total JVM heap size = Size of the young generation + Size of the old generation + Size of the permanent generation. In most cases, the permanent generation has a fixed size of 64 MB. Therefore, if you increase the size of the young generation, the size of the old generation is reduced. The value of -Xmn significantly affects system performance. We recommend that you set this option to 3/8 of the total heap size.

-Xss

Sets the stack size of the thread.

-Xss128k sets the stack size of each thread to 128 KB.

Note

If you use JDK 5.0 or later, set this option to 1 MB. If you use earlier JDK versions, set this option to 256 KB. You can adjust the stack size of each thread based on your business requirements. In the case of the same physical memory size, a smaller value of this option indicates more threads. The number of threads in a process is limited by your OS. In most cases, a process can have 3,000 to 5,000 threads.

-XX:NewRatio=n

Sets the ratio of the young generation to the old generation.

-XX:NewRatio=4 sets the ratio of the young generation to the old generation to 1:4. The size of the young generation is the combined size of the eden and survivor spaces. The permanent generation is excluded. In this case, the young generation occupies 1/5 of the heap.

-XX:SurvivorRatio=n

Sets the ratio of the eden space to the two survivor spaces in the young generation.

-XX:NewRatio=4 sets the ratio of the eden space to the two survivor spaces in the young generation to 2:4. In this case, each survivor space occupies 1/6 of the young generation.

-XX:MaxPermSize=n

Sets the maximum size of the permanent generation.

-XX:MaxPermSize=16m sets the maximum size of the permanent generation to 16 MB.

-XX:MaxTenuringThreshold=n

Sets the age of the objects in the young generation.

-XX:MaxTenuringThreshold=0 sets the age of the objects in the young generation to 0.

  • In this case, the objects in the young generation bypass the survivor spaces and overflow directly into the old generation. This improves the efficiency of applications whose old generation sizes are large.

  • If you set this option to a large value, the objects in the young generation are copied multiple times in the survivor spaces. This increases the period of time during which the objects stay in the young generation. In addition, frequent garbage collections may occur in the young generation.

Configure garbage collectors

Common garbage collector (GC) options for applications that require high throughput

Option

Description

Example

-XX:+UseParallelGC

Instructs the JVM to use the parallel GC for the young generation.

Example: -Xmx3800m -Xms3800m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20. In this example, -XX:+UseParallelGC specifies that the young generation uses the parallel GC and the old generation uses the serial GC.

-XX:ParallelGCThreads

Sets the number of threads that can be used at the same time by the parallel GC for garbage collection.

Note

We recommend that you set the number of threads and the number of GCs to the same value.

Example: -Xmx3800m -Xms3800m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20. In this example, -XX:ParallelGCThreads=20 specifies that 20 threads can be used at the same time for garbage collection.

-XX:+UseParallelOldGC

Instructs the JVM to use the parallel GC for the old generation.

Note

If you use JDK 6.0, you can select the parallel GC for the old generation.

Example: -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseParallelOldGC. In this example, -XX:+UseParallelOldGC specifies that the old generation uses the parallel GC for garbage collection.

-XX:MaxGCPauseMillis

Sets the maximum pause time of each garbage collection for the young generation. The JVM automatically adjusts the size of the young generation to maintain the value of this option.

Example: -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:MaxGCPauseMillis=100. In this example, -XX:MaxGCPauseMillis=100 sets the maximum pause time of each garbage collection to 100 ms.

-XX:+UseAdaptiveSizePolicy

Enables the adaptive size policy. If you configure this option, the parallel GC automatically adjusts the size of the young generation and the ratio of the survivor spaces to achieve the lowest latency or optimal garbage collection frequency. We recommend that you always enable the adaptive size policy for the parallel GC.

-Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:MaxGCPauseMillis=100 -XX:+UseAdaptiveSizePolicy

Common GC options for applications that require low latency

Option

Description

Example

-XX:+UseConcMarkSweepGC

Instructs the JVM to use the concurrent mark sweep (CMS) GC for the old generation.

Note

If you configure the -XX:+UseConcMarkSweepGC option, we recommend that you also configure the -Xmn option for the young generation.

-Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC

-XX:+UseParNewGC

Instructs the JVM to use the parallel GC for the young generation.

The parallel GC and the CMS GC can be used for the young generation at the same time. If you use JDK 5.0 or later, the JVM automatically configures this option based on your system configurations. In this case, you do not need to configure this option.

-Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC

-XX:CMSFullGCsBeforeCompaction

The CMS GC does not compact or manage the heap space. Therefore, fragmentation occurs after the CMS GC runs for a specific period of time. As a result, the running efficiency of applications may decrease. This option specifies the number of times that a CMS GC must run through the heap before the heap space is compacted or managed.

Example: -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseConcMarkSweepGC -XX:CMSFullGCsBeforeCompaction=5 -XX:+UseCMSCompactAtFullCollection. In this example, -XX:CMSFullGCsBeforeCompaction=5 specifies that the CMS GC must run through the heap five times before the CMS GC can compact or manage the heap space.

-XX:+UseCMSCompactAtFullCollection

Enables the compaction for the old generation heap.

Note

If you enable this option, the fragmentation problem can be prevented, but the system performance may be degraded.

-Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseConcMarkSweepGC -XX:CMSFullGCsBeforeCompaction=5 -XX:+UseCMSCompactAtFullCollection

Common verbose GC options

Option

Description

-XX:+PrintGC

Prints GC logs.

-XX:+PrintGCDetails

Prints GC details.

-XX:+PrintGCTimeStamps

Prints the GC timestamp from the time when the JVM starts up to the current date. Example:

0.855: [GC (Allocation Failure) [PSYoungGen: 33280K->5118K(38400K)] 33280K->5663K(125952K), 0.0067629 secs] [Times: user=0.01 sys=0.01, real=0.00 secs]

-XX:+PrintGCDateStamps

Prints the GC timestamp as a date. Example:

2022-01-27T16:22:20.885+0800: 0.299: [GC pause (G1 Evacuation Pause) (young), 0.0036685 secs]

-XX:+PrintHeapAtGC

Prints the heap information before and after garbage collection.

-Xloggc:../logs/gc.log

Sets the output path of the log files.