Learn how to configure JVM stack memory and optimize memory usage to avoid OOM issues.
Configuration reference of stack memory
JVM configuration reference
The following configuration values are for reference only. Adjust the values based on stress tests to optimize system performance.
JVM configuration item | Description | 1C 2G | 2C 4G | 4C 8G | 8C 16G |
-Xms | The initial size of the heap memory | 1G | 2560M | 4G | 10G |
-Xmx | The maximum size of the heap memory | 1G | 2560M | 4G | 10G |
-Xmn | The size of the young generation space | 500M | 1200M | 2G | 5G |
-Xss | The size of the thread stack space (Default value for JDK 8: 1M) | 1M | 1M | 1M | 1M |
-XX:MetaspaceSize | The initial size of the metaspace | 128M | 256M | 384M | 512M |
-XX:MaxMetaspaceSize | The maximum size of the metaspace | 128M | 256M | 384M | 512M |
-XX:MaxDirectMemorySize | The maximum size of the off-heap memory | 256M | 256M | 1G | 1G |
-XX:ReservedCodeCacheSize | The size of the code cache | 64M | 128M | 256M | 256M |
Typical configuration parameters for heap and stack size
Configuration parameter | Description | Example |
| Sets the maximum heap size. |
|
| Sets the minimum heap size. |
|
| Sets the size of the young generation. |
|
| Sets the stack size of the thread. |
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. With the same physical memory size, a smaller value for this option allows more threads. The number of threads in a process is limited by your operating system. In most cases, a process can have 3,000 to 5,000 threads. |
| Sets the ratio of the young generation to the old generation. |
|
| Specifies the ratio of the eden space to the two survivor spaces in the young generation. |
|
| Sets the maximum size of the permanent generation. |
|
| Sets the age of the objects in the young generation. |
|
Configure garbage collectors
Typical configuration parameters for throughput-oriented GC
Configuration parameter | Description | Example |
| Instructs the JVM to use the parallel GC for the young generation. |
|
| Specifies the number of threads that can be used simultaneously by the parallel GC for garbage collection. Note We recommend configuring this value to equal the number of processors. |
|
| 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. |
|
| 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. |
|
| 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 best latency or optimal garbage collection frequency. We recommend that you always enable the adaptive size policy for the parallel GC. |
|
Typical configuration parameters for response time-oriented GC
Configuration parameter | Description | Example |
| Instructs the JVM to use the concurrent mark sweep (CMS) GC for the old generation. Note If you configure |
|
| Specifies 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. |
|
| 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. |
|
| 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. |
|
Typical configuration parameters for auxiliary GC
Configuration parameter | Description |
| The option to print GC logs. |
| The option to print GC details. |
| The option to print the GC timestamp from the time when the JVM starts up to the current date. See the following example:
|
| The option to print the GC timestamp as a date. See the following example:
|
| The option to print the heap information before and after garbage collection. |
| The option to specify the output path of the log files. |