JVM performance tuning Detailed

Earlier we learned the entire JVM series, the ultimate goal is not just to understand the basics of the JVM, JVM tuning but also to be ready to do. This article lead us to learn JVM performance tuning.

Performance Tuning

Tuning comprises a plurality of levels, such as: architecture tuning, tuning the code, the JVM tuning, tuning the database, the operating system calls excellent.

Architecture tuning and tuning the code is the basis of the JVM tuning, tuning the architecture of the greatest impact on the system.

Performance Tuning essentially according to the following steps: a clear objective optimization, find performance bottlenecks, performance tuning, data obtained by monitoring data and statistical tools, to confirm whether the goal.

When to tune JVM

Following conditions are met, we need to consider tuning the JVM:

  • Heap memory (years old) continued to rise to reach maximum memory values;
  • Full GC number of frequent;
  • GC pause time is too long (more than 1 second);
  • OutOfMemory and other memory applications appear abnormal;
  • There are applications use the local cache and take up a lot of memory space;
  • System throughput and response performance is not high or lowered.

Basic Principles tuning JVM

JVM tuning is a means, but not necessarily all problems can be solved by tuning the JVM, therefore, during the JVM tuning, we have to follow some guidelines:

  • Most Java applications do not require JVM optimization;
  • Most GC causes the problem is the code-level problems caused by (the code level);
  • Prior to on-line, the machine should be considered to the optimal parameter settings JVM;
  • Reducing the number (code level) to create objects;
  • Reduce the use of global variables and large objects (code level);
  • Priority architecture tuning and code tuning, JVM optimization is the means (the code, the architectural level) of last resort;
  • GC analysis of the situation better than the optimized code optimized JVM parameters (code level);

Through the above principles, we find that, in fact, is the most efficient means to optimize architecture and code-level optimization, and JVM optimization is the last resort, it can be said that the last time "squeeze" on the server configuration.

JVM tuning goals

Tuning the ultimate goal is to make an application with minimal hardware cost to carry greater throughput. jvm tuning is mainly aimed at collecting garbage collector performance optimization, so that applications running on the virtual machine to use less memory and delay get greater throughput.

  • Delay: GC and GC pause low low frequencies;
  • Low memory footprint;
  • High throughput;

Among them, any of these properties to improve performance, almost always at the expense of other properties at the expense of performance loss, can not have both. According to the importance of specifically identified in the business.

JVM tuning quantization target

The following shows the number of quantization target JVM tuning Reference Example:

  • Heap Memory usage <= 70%; = "" <= "" li = "">
  • Old generation memory usage <= 70%; = "" <= "" li = "">
  • avgpause <= 1秒;="" <="" li="">
  • Full gc number 0 or avg pause interval> = 24 hours;

Note: JVM tuning quantitative targets for different applications is not the same.

JVM tuning step

Typically, JVM tuning may be performed by the following steps:

  • GC analysis of the log and dump files to determine whether the need to optimize, identify bottlenecks point;
  • Determining a target quantization JVM tuning;
  • JVM tuning parameter is determined (adjusted according to the parameter history JVM);
  • Sequentially tuning memory, latency and throughput targets;
  • Observation of differences before and after tuning;
  • Continuous analysis and adjustment until you find a suitable JVM parameters;
  • Find the most suitable parameters, these parameters will be applied to all servers, and follow up.

Above steps, certain steps that require multiple iterations ever done. Generally from memory usage to meet the needs of the program began, followed by a time delay requirements, and finally throughput requirements, this step should be based on continuous optimization, each step is the basis for the next step, not retrograde.

JVM arguments

JVM tuning the most important tool is the JVM parameters. First look JVM parameters related content.

-XX parameter is called unstable parameters, such parameters set easily cause differences in performance JVM, so there is great instability JVM. If this parameter is set reasonable will greatly improve the performance and stability of the JVM.

Unstable parameter syntax rules include the following.

Boolean parameter values:

  • -XX:+
  • -XX:-

Numeric parameters:

  • -XX:

Parameter Type Value String:

  • -XX:

JVM tuning parameter analysis and

Parameters such as the following example:

-Xmx4g –Xms4g –Xmn1200m –Xss512k -XX:NewRatio=4 -XX:SurvivorRatio=8 -XX:PermSize=100m -XX:MaxPermSize=256m -XX:MaxTenuringThreshold=15复制代码

The above is an example and previous versions Java7, permanent generation parameters -XX in Java8: PermSize and -XX: MaxPermSize has failed. This has been talked about in the previous section.

Parameters Resolution:

  • -Xmx4g: heap maximum of 4GB.
  • -Xms4g: initialize heap memory size is 4GB.
  • -Xmn1200m: Set the size of the young generation is 1200MB. After the increase in the young generation, it will reduce the size of the old generation. This value greater impact on system performance, Sun official recommended configuration for the entire heap of 3/8.
  • -Xss512k: Sets the stack size for each thread. JDK5.0 after each thread stack size is 1MB, before each thread stack size is 256K. Thread should be adjusted according to the desired application memory size. At the same physical memory, reducing this value can generate more threads. However, the number of operating system threads within a process still limited, not unlimited generation, experience in 3000 and 5000.
  • -XX: NewRatio = 4: Set the young generation (including Eden and two Survivor areas) the ratio of the old generation (excluding permanent generation). Is set to 4, the share of the young generation and the old generation ratio of 1: 4, the young generation accounts for 1/5 of the entire stack
  • -XX: SurvivorRatio = 8: Set the size of the area ratio of Eden Survivor young generation area. Is set to 8, the ratio of the two regions with a Survivor Eden zone is 2: 8, a Survivor young generation area 1/10 of the total
  • -XX: PermSize = 100m: initializing the size of the permanent generation of 100MB.
  • -XX: MaxPermSize = 256m: set the permanent generation size is 256MB.
  • -XX: MaxTenuringThreshold = 15: Setting the maximum age of garbage. If set to 0, then the younger generation of the object without Survivor areas, directly into the old generation. For more of the old generation of applications that can improve efficiency. If this value is set to a large value, then the young generation objects will be copied many times in Survivor areas, which can increase the object and then the young generation of survival time, an increase in the introduction to the young generation namely recycling.

The new generation, old generation, permanent generation parameters, if not specified, the virtual machine to automatically select the appropriate value, also automatically adjusted based on system overhead.

Tunable parameters:

-Xms: Initialization heap size, default is 1/64 of physical memory (less than 1GB).

-Xmx: maximum heap memory. Default (parameter may be adjusted MaxHeapFreeRatio) of free memory heap is greater than 70%, JVM stack will decrease until the minimum limit -Xms.

-Xmn: the size of the new generation, Eden region comprising two Survivor areas.

-XX: SurvivorRatio = 1: Eden region and a Survivor area ratio of 1: 1.

-XX: MaxDirectMemorySize = 1G: Direct memory. News java.lang.OutOfMemoryError: Direct buffer memory exception can increase this value.

-XX: + DisableExplicitGC: prohibited run explicitly call System.gc () to trigger fulll GC.

Note: GC trigger timing of Java RMI 86400 can be controlled by configuring the trigger time -Dsun.rmi.dgc.server.gcInterval =.

-XX: CMSInitiatingOccupancyFraction = 60: Memory reclamation threshold years old, the default value is 68.

-XX: ConcGCThreads = 4: CMS garbage collector thread of parallel lines, the number of CPU cores is recommended.

-XX: ParallelGCThreads = 8: new generation of parallel threads collector.

-XX: MaxTenuringThreshold = 10: Setting the maximum age of garbage. If set to 0, then the younger generation of the object without Survivor areas, directly into the old generation. For more of the old generation of applications that can improve efficiency. If this value is set to a large value, then the young generation objects will be copied many times in Survivor areas, which can increase the object and then the young generation of survival time, an increase in the introduction to the young generation namely recycling.

-XX: CMSFullGCsBeforeCompaction = 4: After specifying how many times fullGC, were tenured region memory space compression.

-XX: CMSMaxAbortablePrecleanTime = 500: When performing a pre-cleaning phase abortable-preclean reach this time will end.

When set, if the performance overhead of concern, then, should be the initial value of the permanent generation with the maximum set to the same value as the size of the permanent generation needs to be adjusted to achieve FullGC.

Memory optimization example

When the JVM is stable, triggered FullGC we usually get the following information:

image

Above gc log when fullGC occurred, occupying the entire application stack, and the GC time. To be more precise to be repeated collection, calculating an average value. Or using the longest time FullGC be estimated. The figure above, the space occupied by the old year in 93168kb (about 93MB), this data set is active years old space. The other heap space allocated to carry out based on the following rules.

  • java heap: -Xms parameters and -Xmx, proposes to extend to the space after 3-4 years old times FullGC occupied.
  • Permanent Generation: -XX: PermSize and -XX: MaxPermSize, proposes to extend to a permanent post with the space occupied by 1.2-1.5 times FullGc.
  • New Generation: -Xmn, proposes to extend to the old space years after 1-1.5 times FullGC occupied.
  • Years old: after 2-3 times FullGC years old footprint.

Based on the above rules, the parameters are defined as follows:

java -Xms373m -Xmx373m -Xmn140m -XX:PermSize=5m -XX:MaxPermSize=5m复制代码

Delay optimization example

Delay optimization, you first need to understand the delayed demand and indicators of what tunable.

  • Acceptable application average dead time: Minor this time and the measured
  • GC duration were compared. Minor GC acceptable frequency: Minor
  • GC compared with the value of the frequency may be tolerated.
  • The maximum acceptable pause time: The maximum pause time is compared to the duration of FullGC the worst case.
  • The frequency of occurrence of the maximum acceptable pause: Basic is FullGC frequency.

Among them, the average dead time and the maximum pause time, the most important to the user experience. For the above indicators, data collection include: the duration of MinorGC, the number of MinorGC statistics, the worst duration FullGC, the worst case, FullGC frequency.

image

As shown above, the average duration of 0.069 seconds Minor GC, MinorGC a frequency of 0.389 seconds.

The new generation of larger space, the GC GC Minor longer time, the lower the frequency. If you want to reduce the length of its duration, it is necessary to reduce the size of its space. If you want to reduce the frequency, it is necessary to increase the size of its space.

Here in order to reduce by 10% the size of the new generation of space, to reduce the delay time. In this process, it should be kept and who's old generation size does not change. Tuning parameters change as follows:

java -Xms359m -Xmx359m -Xmn126m -XX:PermSize=5m -XX:MaxPermSize=5m复制代码

Tuning throughput

Tuning throughput is mainly based on the throughput requirements of the application comes, there should be a comprehensive application throughput indicator, the indicator is based on the needs and test the entire application and its ramifications.

Assess whether the current throughput and target huge gap, if around 20%, you can modify parameters, increase memory, debugging again from scratch, you need to consider if the great from the entire application level, as well as design goals are the same, and re-evaluate the throughput target .

For the garbage collector is to enhance the throughput performance tuning goal is to avoid as much as possible or FullGC or Stop-The-World compression garbage collection (CMS) rarely occur, because these two approaches will result in lower throughput applications . Try to recover more objects in MinorGC stage, avoid lifting an object too fast to the old era.

Tuning Tools

Means GCViewer log analysis tool, the analysis can be very intuitive to be adjusted advantages. It can be analyzed from the following aspects:

Memory, analysis Totalheap, Tenuredheap, Youngheap memory usage and other metrics, in theory, the smaller the better memory usage;

Pause, analysis Gc pause, Fullgc pause, Total pause three major items in each index, the better the theoretical number of GC, GC long time as small as possible;

Original link: " the JVM performance tuning Detailed "

Reference article:

(1)https://blog.csdn.net/jisuanjiguoba/article/details/80176223(2)https://juejin.im/post/59f02f406fb9a0451869f01c

"Interviewer" series:


New Horizons program : exciting and growth are not to be missed

New Horizons program - micro-channel public number

Guess you like

Origin juejin.im/post/5dc8d0ea518825592c566a5d