JVM-JVM performance tuning

Goals and Methods of JVM Performance Tuning

The goal of JVM performance tuning is to enable the JVM to use computer resources more efficiently when running Java applications, so as to improve the performance and responsiveness of the applications. Specifically, the goals of JVM performance tuning can include the following aspects:

  1. Improve application throughput: reduce JVM overhead and improve application throughput by optimizing JVM memory management, garbage collection, and thread management.
  2. Reduce application delay: By optimizing the JVM garbage collection strategy and adjusting the size of the thread pool, the application delay is reduced and the user experience is improved.
  3. Reduce the memory usage of JVM: By optimizing the memory management strategy of JVM, reduce the memory usage of JVM and improve the stability of the system.

The method of JVM performance tuning can include the following aspects:

  1. Adjust the memory parameters of the JVM: By adjusting the memory parameters of the JVM (such as -Xms, -Xmx, -Xmn, etc.), control the size of the heap memory and non-heap memory of the JVM, and avoid problems such as memory leaks and OOM (Out Of Memory).
  2. Select the appropriate garbage collector : JVM provides a variety of garbage collectors (such as Serial, Parallel, CMS, G1, etc.), select the appropriate garbage collector according to the characteristics of the application, and adjust its parameters to achieve the best garbage collector recycling effect.
  3. Adjust the size of the thread pool: By adjusting the size of the thread pool, avoid too many or too few threads, thereby improving the concurrent processing capability of the application.
  4. Use performance analysis tools: Use performance analysis tools (such as JProfiler, VisualVM, Arthas tools; jstst jstack jmap command, etc.) to analyze the application, find out bottlenecks and performance problems, and optimize them.
  5. Optimize code: optimize code (such as avoid frequent object creation, avoid excessive method calls, etc.), reduce JVM overhead, and improve application performance.

Metrics and tools for JVM performance tuning

The indicators for JVM performance tuning are important criteria for measuring JVM performance. Common indicators include the following aspects:

  1. Heap memory usage: Indicates the usage of JVM heap memory, which should usually be controlled below 70% to avoid memory problems such as OOM.
  2. Garbage collection time: Indicates the time spent by the JVM for garbage collection. Excessive garbage collection time will affect the performance of the application.
  3. Number of threads: Indicates the number of threads currently running in the JVM. Too many or too few threads will affect the performance of the application.
  4. CPU usage: Indicates the CPU usage of the JVM. Excessive CPU usage will affect the performance of the application.
  5. Response time: Indicates the time for the application to respond to the request. The response time should be shortened as much as possible to improve user experience.

Commonly used JVM performance tuning tools include the following aspects:

  1. JVisualVM: It is a performance analysis tool that comes with the JDK. It can be used to monitor the heap memory, garbage collection, threads, etc. of the JVM, and can perform thread analysis, memory analysis, and other operations.
  2. JProfiler: It is a commercial performance analysis tool that can provide more detailed performance analysis and optimization, and supports multiple JVM environments and application types.
  3. GCViewer: It is an open source garbage collection log analysis tool that can be used to analyze the GC log of the JVM to help users understand the situation of garbage collection.
  4. VisualGC: It is a visual JVM performance monitoring tool that can be used to monitor JVM memory, garbage collection, etc. in real time.
  5. Perf: It is a performance analysis tool that comes with the Linux system. It can be used to monitor the CPU, memory, I/O, etc. of the system, and it can also be used to monitor the performance of the JVM.
  6. MAT: MAT is a Java memory analyzer, the full name is Eclipse Memory Analyzer Tool. MAT can help developers quickly analyze the memory usage of Java applications, find out problems such as memory leaks and memory waste, and provide optimization suggestions.

Tips and Advice for JVM Performance Tuning

Here are some tips and advice for JVM performance tuning:

  1. Choose an appropriate garbage collector: Choose an appropriate garbage collector according to the characteristics of the application, and adjust its parameters according to the actual situation. Different garbage collectors are suitable for different application scenarios.
  2. Control the heap memory size: By adjusting the JVM heap memory size, avoid problems such as memory leaks and OOM. It is generally recommended to set the -Xmx parameter to about 70% of the physical memory.
  3. Avoid creating too many objects: Try to avoid creating objects frequently in the loop, and you can use technologies such as object pools to optimize the code.
  4. Use the thread pool: By using the thread pool, you can avoid the situation of too many or too few threads, and improve the concurrent processing capability of the application.
  5. Avoid excessive method calls: Excessive method calls will increase the overhead of the JVM and affect the performance of the application. Code can be optimized through techniques such as inlining.
  6. Avoid excessive synchronization: Excessive synchronization will reduce the concurrent processing capability of the application and affect the performance of the application. Code can be optimized by using techniques such as lock separation.
  7. Use performance analysis tools: Use performance analysis tools to help locate performance bottlenecks and problems, and optimize them. Commonly used performance analysis tools include JProfiler, VisualVM, GCViewer, etc.
  8. Regular performance testing: Regular performance testing to discover performance problems and optimize them in time. Performance testing can be done using tools such as JMeter.
  9. Optimize JVM startup parameters: By adjusting JVM startup parameters, the performance and stability of JVM can be improved. Commonly used JVM startup parameters include -Xms, -Xmx, -XX:+UseG1GC, etc.

In short, JVM performance tuning is a complex process that needs to be optimized according to specific applications. Through reasonable selection of garbage collectors, control of heap memory size, code optimization and other technical means, the performance and stability of JVM can be improved, and the performance and responsiveness of applications can be improved.

JVM performance tuning in practice

JVM related parameters

JVM parameters are divided into 3 categories:

  • Standard parameters (-), all JVMs must support the functions of these parameters, and are backward compatible
  • Non-standard parameters (-X), the default JVM implements the functions of these parameters, but does not guarantee that all JVM implementations are satisfied, and does not guarantee backward compatibility
  • Unstable parameters (-XX), such parameters will be different for each JVM implementation, and may not be supported in the future, so use with caution

JVM standard parameters

  1. -server/-client: Specifies the JVM operating mode, -server means server mode, optimizes long-running server applications, -client means client mode, optimizes startup speed and response time.
  2. -classpath/-cp: Specifies the classpath for finding class files and resource files.
  3. -Dproperty=value: Set the system property, which can be obtained through the System.getProperty() method in the application.
  4. -verbose:class: Display class loading information, including class name, loading time, and class loader.
  5. -version: Display the JVM version information.
  6. -showversion: Displays JVM version information and build information.
  7. -help: Display help information for JVM command line parameters.

Stack memory setting parameters

  1. -Xms: initial heap size, the default value is 1/64 of memory
  2. -Xmx: maximum heap size, the default value is 1/4 of memory
  3. -Xmn: the initial size of the young generation, the default value of the -Xmn parameter is the ratio of the young generation to the old generation is 1:2, that is, the young generation accounts for 1/3 of the entire heap
  4. -XX:NewSize: Set the size of the young generation; the difference between -Xmn and -XX:NewSize: the scope of the parameters is different: the -Xmn parameter only acts on the young generation, while the -XX:NewSize parameter acts on the young generation and the Survivor area; the parameter The default values ​​are different: the default value of the -Xmn parameter is calculated based on the heap size, and the default value of the -XX:NewSize parameter is 0, which means that the JVM will automatically calculate the size of the young generation and the Survivor area
  5. -XX:MaxNewSize: the maximum value of the young generation
  6. -XX:NewRatio: The ratio of the young generation (including Eden and two Survivor areas) to the old generation (excluding the permanent generation)
  7. -XX:SurvivorRatio: The size ratio of the Eden area to the Survivor area, the default value is set to 8, then the ratio of two Survivor areas to one Eden area is 2:8, and one Survivor area accounts for 1/10 of the entire young generation
  8. -Xss: the stack size of each thread, the default value of each thread stack size after JDK5.0 is 1M
  9. -XX:ThreadStackSize: used to set the size of the thread stack -Xss and -XX:ThreadStackSize difference: the scope of parameters is different: -XX:ThreadStackSize parameter is used to set the stack size of all threads, and -Xss parameter is used to set a single thread the stack size of
  10. -XX:LargePageSizeInBytes: Used to set the size of large pages. Enabling huge pages can improve the efficiency of memory access, thereby improving application performance and responsiveness. However, it should be noted that enabling large pages may cause memory fragmentation problems, which will affect memory usage efficiency and stability.
  11. -XX:MaxTenuringThreshold: Used to set the maximum age for objects to survive between the young generation and the old generation. The young generation is a part of the Java heap memory used to store newly created objects. When the young generation is full, a garbage collection is triggered to move the surviving objects to the old generation. The default value is 15, which means that the object will be moved to the old generation after surviving up to 15 garbage collections.
  12. -Xnoclassgc: Used to control whether the JVM recycles useless classes during garbage collection.
  13. -XX:SoftRefLRUPolicyMSPerMB: Time limit for setting the LRU policy of soft reference objects. The default value is 1000, which means that soft reference objects can survive for 1 second per MB of memory space. You can adjust the LRU policy of soft reference objects by setting the -XX:SoftRefLRUPolicyMSPerMB parameter, thereby affecting the recycling policy and memory usage of soft reference objects.
  14. -XX:PretenureSizeThreshold: It is used to set the threshold size for objects in the new generation memory area to directly enter the old generation memory area. The default value is 0, which means that direct access to the old age is disabled.
  15. -XX:TLABWasteTargetPercent: Used to set the waste threshold percentage of the thread local allocation buffer (Thread Local Allocation Buffer, TLAB). The default value is 1, which means that the wasted space in the thread-local allocation buffer cannot exceed 1% of the total space.
  16. -XX:+CollectGen0First: Used to control whether the JVM first reclaims the new generation memory area when performing Full GC.

Metadata setting parameters

  1. -XX:MetaspaceSize: Set the initial size of the metadata area.
  2. -XX:MaxMetaspaceSize: Set the maximum size of the metadata area.
  3. -XX:MinMetaspaceFreeRatio: Set the minimum free space ratio of the metadata area.
  4. -XX:MaxMetaspaceFreeRatio: Set the maximum free space ratio of the metadata area.

JIT setup parameters

Garbage collector setting parameters

parameter name meaning Defaults
-XX:+UseParallelGCFull GC uses parallel MSC Select the garbage collector as a parallel collector. This configuration is only valid for the young generation. That is, under the above configuration, the young generation uses concurrent collection, while the old generation still uses serial collection.
-XX:+UseParNewGC Set young generation to collect in parallel  Can use JDK5.0 or above at the same time as CMS collection, JVM will set itself according to system configuration, so there is no need to set this value
-XX:ParallelGCThreads The number of threads of the parallel collector is best configured to be equal to the number of processors, which is also applicable to CMS
-XX:+UseParallelOldGC The garbage collection method of the old generation is parallel collection (Parallel Compacting)  This is the parameter option that appeared in JAVA 6
-XX:MaxGCPauseMillis Maximum time for each young generation garbage collection (maximum pause time)  If this time cannot be met, the JVM will automatically adjust the young generation size to meet this value.
-XX:+UseAdaptiveSizePolicy Automatically select the size of the young generation area and the corresponding proportion of the Survivor area  After setting this option, the parallel collector will automatically select the size of the young generation area and the corresponding proportion of the Survivor area to achieve the minimum corresponding time or collection frequency specified by the target system. This value is recommended to be turned on when the parallel collector is used.
-XX:GCTimeRatio Set the percentage of garbage collection time to program running time. The formula is 1/(1+n)
-XX:+ScavengeBeforeFullGCFull Call YGCtrue before GC Do young generation GC prior to a full GC.

CMS related parameters

  1. -XX:+UseConcMarkSweepGC: use CMS memory collection
  2. -XX:+AggressiveHeap : Used to enable a performance-based heap memory configuration. After enabling this parameter, the JVM will automatically adjust the heap memory size and garbage collection strategy according to the current system hardware configuration and memory usage to achieve the best performance and memory utilization.
  3. -XX:CMSFullGCsBeforeCompaction : How many times to perform memory compression
  4. -XX:+CMSParallelRemarkEnabled: reduce mark pause
  5. -XX+UseCMSCompactAtFullCollection: Compression of the old generation during FULL GC
  6. -XX:+UseCMSInitiatingOccupancyOnly: start CMS collection with manual definition initialization definition
  7. -XX:CMSInitiatingOccupancyFraction=70: Use cms as garbage collection to start CMS collection after 70% usage

GC log setting parameters

  1. -XX:+PrintGC: Output format: [GC 118250K->113543K(130112K), 0.0094143 secs] [
    Full GC 121376K->10414K(130112K), 0.0650971 secs]
  2. -XX:+PrintGCDetails: output format: [GC [DefNew: 8614K->781K(9088K), 0.0123035 secs] 118250K->113543K(130112K), 0.0124633 secs] [GC [DefNew: 8614K->8614K(90 88K),
    0.0000665 secs][Tenured: 112761K->10414K(121024K), 0.0433488 secs] 121376K->10414K(130112K), 0.0436268 secs]
  3. -XX:+PrintGCTimeStamps
  4. -XX:+PrintGC:PrintGCTimeStamps: Can be mixed with -XX:+PrintGC -XX:+PrintGCDetails Output
    format: 11.851: [GC 98328K->93620K(130112K), 0.0082960 secs]
  5. -XX:+PrintGCApplicationStoppedTime: Prints the time the program was suspended during garbage collection. Can be mixed with the above
  6. -XX:+PrintGCApplicationConcurrentTime: Print the uninterrupted execution time of the program before each garbage collection. It can be mixed with the above
  7. -XX:+PrintHeapAtGC : Print detailed stack information before and after GC
  8. -Xloggc:filename: Record relevant log information to a file for analysis. Use with the above
  9. -XX:+PrintClassHistogram
  10. -XX:+PrintTLAB
  11. -XX:+PrintTenuringDistribution: View the threshold of the new survival cycle after each minor GC

other

  1. -XX:+UseFastAccessorMethods: Used to enable fast accessor methods. The fast accessor method is an optimization technique that can improve the efficiency of accessing the properties of an object by directly accessing the fields of the object instead of accessing the properties of the object through getter and setter methods. The argument is true to enable quick accessor methods. It should be noted that enabling the quick accessor method may affect the maintainability and scalability of the program, because it will expose the internal implementation details of the object, making the code more dependent on the specific implementation of the object. Therefore, it needs to be selected and adjusted according to specific application scenarios and requirements.
  2. -XX:+DisableExplicitGC: Turn off System.gc()
  3. -XX:+UseBiasedLocking: performance improvement of the locking mechanism
  4. -XX:MaxDirectMemorySize: Set the maximum size of direct memory

Actual configuration

To be updated

Guess you like

Origin blog.csdn.net/BASK2312/article/details/131359781