What is the role of the JVM parameters -Xms and -Xmx

The role of JVM parameters -Xms and -Xmx

The JVM parameters -Xmsand -Xmxare used to set the initial and maximum size of the Java Virtual Machine (JVM) heap. Specifically:

  1. -XmsThe parameter is used to set the initial size of the JVM heap, that is, the minimum amount of memory allocated to the Java heap when the JVM starts. The default is usually 1/64 of physical memory. It is generally recommended to set it to 1/4 or 1/2 the maximum heap size.
  2. -XmxThe parameter is used to set the maximum size of the JVM heap, that is, the maximum amount of memory that the Java heap can use. OutOfMemoryError occurs when the Java heap reaches the maximum amount of memory. The default is usually 1/4 of physical memory. A general recommendation is to set it to 70% to 80% of the system's available memory.

Setting an appropriate heap memory size can avoid OOM errors in the program due to insufficient memory, and can also improve the performance and efficiency of the program. If the heap memory is insufficient, the JVM will continuously trigger garbage collection, resulting in poor program performance. If the heap memory is too large, system resources will be wasted.

It should be noted that the heap memory is only a part of the JVM memory, including stack memory, method area memory, and local method stack memory. Therefore, when setting the heap memory size, it is necessary to comprehensively consider the size of the entire JVM memory and the actual needs of the program. In addition, different applications and operating systems may require different heap memory sizes, which need to be adjusted according to specific situations.

Typically, -Xmsand can -Xmxbe set to the same value to avoid dynamic adjustment of the heap size. For example, the following JVM parameter sets both the initial and maximum size of the JVM heap to 2GB:

java -Xms2g -Xmx2g Main
-- main代表应用程序

Some commonly used JVM tuning commands

-Xms: 指定 Java 程序启动时初始堆大小。
-Xmx: 指定 Java 程序最大堆大小。
-Xmn: 指定年轻代大小。
-XX:PermSize: 指定永久代初始大小。
-XX:MaxPermSize: 指定永久代最大大小。
-XX:+UseConcMarkSweepGC: 使用 CMS 垃圾回收器。
-XX:+UseParallelGC: 使用并行垃圾回收器。
-XX:ParallelGCThreads: 设置并行垃圾回收器的线程数。
-XX:+DisableExplicitGC: 禁止使用 System.gc() 进行垃圾回收。
-XX:+HeapDumpOnOutOfMemoryError: 当发生 OutOfMemoryError 时自动生成堆转储快照文件。
-XX:HeapDumpPath: 指定堆转储文件的路径。
-XX:+PrintGCDetails: 打印垃圾回收的详细信息。
-XX:+PrintGCTimeStamps: 打印垃圾回收的时间戳信息。
-XX:+PrintHeapAtGC: 打印垃圾回收前后的堆信息。
-XX:SurvivorRatio: 设置年轻代中 Eden 区与 Survivor 区的大小比例。

Guess you like

Origin blog.csdn.net/qq_42133976/article/details/130417582