适用于JDK 7, 8
utf8
-Dfile.encoding=UTF8
Tuning
-Xms, -Xmx
-Xms4g -Xmx4g
-Xms
Setting initial and minimum heap size
-Xmx
Setting maximum heap size
The -Xms
and -Xmx
parameters define the minimum and maximum heap sizes, respectively. Since GC occurs when the generations fill up, throughput is inversely proportional to the amount of the memory available. By default, the JVM grows or shrinks the heap at each GC to try to keep the proportion of free space to the living objects at each collection within a specific range. This range is set as a percentage by the parameters -XX:MinHeapFreeRatio=minimum
and -XX:MaxHeapFreeRatio=maximum
; and the total size bounded by -Xms
and -Xmx
.
Set the values of -Xms and -Xmx equal to each other for a fixed heap size. When the heap grows or shrinks, the JVM must recalculate the old and new generation sizes to maintain a predefined NewRatio.
Setting a low maximum heap value compared to the amount of live data decrease performance by forcing frequent garbage collections.
Oracle recommends setting the minimum heap size (-Xms
) equal to the maximum heap size (-Xmx
) to minimize garbage collections.
-XX:NewRatio=ratio
Ratio of old/new generation sizes. The default value is 2.
By default, the young generation size is controlled by the parameter NewRatio. For example, setting -XX:NewRatio=3
means that the ratio between the young and tenured generation is 1:3. In other words, the combined size of the eden and survivor spaces will be one-fourth of the total heap size.
-XX:PermSize=size, -XX:MaxPermSize=size
PS Old Gen
Sets the maximum permanent generation space size (in bytes). This option was deprecated in JDK 8, and superseded by the -XX:MaxMetaspaceSize option.
-XX:NewSize=size, -XX:MaxNewSize=size
PS Eden Gen
Sets the maximum size (in bytes) of the heap for the young generation (nursery). The default value is set ergonomically.
The NewSize and MaxNewSize parameters control the new generation’s minimum and maximum size. Regulate the new generation size by setting these parameters equal. The bigger the younger generation, the less often minor collections occur.
-XX:SurvivorRatio=ratio
Sets the ratio between eden space size and survivor space size. By default, this option is set to 8.
The SurvivorRatio parameter controls the size of the two survivor spaces. For example, -XX:SurvivorRatio=6
sets the ratio between each survivor space and eden to be 1:6, each survivor space will be one eighth of the young generation. The default for Solaris is 32. If survivor spaces are too small, copying collection overflows directly into the old generation. If survivor spaces are too large, they will be empty. At each GC, the JVM determines the number of times an object can be copied before it is tenured, called the tenure threshold. This threshold is chosen to keep the survivor space half full.
-XX:+PrintTenuringDistribution
Use the option -XX:+PrintTenuringDistribution
to show the threshold and ages of the objects in the new generation. It is useful for observing the lifetime distribution of an application.
Xss
Sets the thread stack size (in bytes). This option is equivalent to -XX:ThreadStackSize
.
https://www.eclipse.org/openj9/docs/xss/
Monitoring
Monitoring and Managing Java SE 6 Platform Applications
jps, jinfo, jmap, jstat, jhat, jstack
jps -m -l -v
jmap -heap pid
JConsole
The JConsole graphical user interface is a monitoring tool that complies with the Java Management Extensions (JMX) specification. JConsole uses the extensive instrumentation of the Java Virtual Machine (Java VM) to provide information about the performance and resource consumption of applications running on the Java platform.
https://docs.oracle.com/en/java/javase/11/management/using-jconsole.html
jvisualvm
VisualVM is a visual tool integrating commandline JDK tools and lightweight profiling capabilities.
Designed for both development and production time use.
MBean
An MBean is a managed Java object, similar to a JavaBeans component, that follows the design patterns set forth in the JMX specification. An MBean can represent a device, an application, or any resource that needs to be managed.
https://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html