JVM startup parameters
-Xms and -Xmx: Initial and Maximum Heap Size
-Xms
is used to set the initial heap size of the JVM at startup, while -Xmx
sets the maximum heap size. These two parameters have a direct impact on application performance, and setting them appropriately can avoid frequent GC (Garbage Collection).
Example:
java -Xms512m -Xmx1024m -jar yourapp.jar
-Xss: Set Thread Stack Size
The -Xss
parameter sets the stack size for each thread. An appropriate stack size can prevent stack overflow errors, especially when the program involves a lot of recursive calls.
Example:
java -Xss512k -jar yourapp.jar
-XX:PermSize and -XX:MaxPermSize: Permanent Generation Memory
-XX:PermSize
and -XX:MaxPermSize
are used to set the memory size for class information, constant pools, etc. Note that in JDK 8 and above, the permanent generation (PermGen) has been replaced by Metaspace, and the related parameters have changed.
Example:
java -XX:PermSize=128m -XX:MaxPermSize=256m -jar yourapp.jar
GC Related
The G1 Garbage Collector is a low-latency garbage collector provided by the JVM, suitable for most server applications. Using the -XX:+UseG1GC
parameter can enable the G1 Garbage Collector and improve GC performance.
Example:
java -XX:+UseG1GC -jar yourapp.jar
To monitor and tune GC behavior, you can use the -XX:+PrintGCDetails
and -XX:+PrintGCDateStamps
parameters to output detailed GC log information. These logs are very helpful for analyzing GC performance issues.
Example:
java -XX:+PrintGCDetails -XX:+PrintGCDateStamps -jar yourapp.jar
The -Xloggc
is a very useful JVM parameter to specify the output file path for GC (Garbage Collection) logs. This parameter can help you record the garbage collection log information to a file for future analysis and debugging.
Example:
java -Xloggc:/path/to/your/gc.log -jar yourapp.jar
Metaspace Related
-XX:MetaspaceSize
-XX:MetaspaceSize
sets the initial size of the Metaspace. When the usage of Metaspace reaches this value, the JVM triggers a garbage collection (GC) to reclaim unused class metadata.
Example:
java -XX:MetaspaceSize=128m -jar yourapp.jar
-XX:MaxMetaspaceSize
-XX:MaxMetaspaceSize
sets the maximum size of the Metaspace. If this parameter is not specified, the Metaspace can grow indefinitely until it exhausts physical memory.
Example:
java -XX:MaxMetaspaceSize=256m -jar yourapp.jar
-XX:MinMetaspaceFreeRatio and -XX:MaxMetaspaceFreeRatio
These parameters control the free space ratio of the Metaspace. -XX:MinMetaspaceFreeRatio
sets the minimum free ratio, and if the free Metaspace falls below this ratio, the JVM will trigger an expansion of the Metaspace. -XX:MaxMetaspaceFreeRatio
sets the maximum free ratio, and if the free Metaspace exceeds this ratio, the JVM will trigger a contraction of the Metaspace.
Example:
java -XX:MinMetaspaceFreeRatio=40 -XX:MaxMetaspaceFreeRatio=70 -jar yourapp.jar
-XX:CompressedClassSpaceSize
-XX:CompressedClassSpaceSize
is used to set the size of the compressed class space, which is a part of the Metaspace used to store class pointers.
Example:
java -XX:CompressedClassSpaceSize=64m -jar yourapp.jar
Class Loading Related
The Java Virtual Machine (JVM) provides various parameters to control class loading behavior. Properly configuring these parameters can help you manage the class loading process better and improve application performance. Here are some commonly used class loading parameters:
-XX:+TraceClassLoading
This parameter is used to trace the class loading process. When enabled, the JVM outputs detailed information about class loading, helping you understand which classes are loaded and when.
Example:
java -XX:+TraceClassLoading -jar yourapp.jar
-XX:+TraceClassUnloading
Corresponding to -XX:+TraceClassLoading
, this parameter traces the class unloading process. It can help diagnose issues related to class unloading, especially useful when debugging and optimizing memory usage.
Example:
java -XX:+TraceClassUnloading -jar yourapp.jar
-XX:InitiatingHeapOccupancyPercent
This parameter sets the heap occupancy threshold, and when it reaches this value, the JVM triggers a global garbage collection. The value is a percentage and is suitable for the G1 Garbage Collector.
Example:
java -XX:InitiatingHeapOccupancyPercent=45 -jar yourapp.jar
-verbose:class
The -verbose:class
parameter outputs detailed information about class loading and unloading, including timestamps and class names for each event. It is very helpful for debugging class loading issues.
Example:
java -verbose:class -jar yourapp.jar
-XX:NativeMemoryTracking
This parameter enables native memory tracking, helping you monitor and analyze the JVM's native memory usage. It supports "off," "summary," and "detail" modes.
Example:
java -XX:NativeMemoryTracking=summary -jar yourapp.jar