The common optimization directions are generally divided intoThree precautionswithThree solutions.

Basic knowledge building

Partition of heap space

Removed jdK7 from JDK8The permanent generationIs replaced byMetaSpaceThis section, in JDK7, stores the string constant pool, static constant pool, and Class meta information in the permanent generation, and is optimized in JDK8 to move the string constant pool, Class static variables, symbol references, and other items to the Heap, and move the Class meta information to an out-of-heap memory area. – Fixed an OOM issue with JVM in JDK7.

Important conceptual points

What does GC stand for? GC refers to three concepts, and you can’t just refer to one of them

  1. Techniques for GC garbage collection
  2. GC garbage collector
  3. The process of GC garbage collection

Mutator, the garbage producer, conducts allocate garbage and free garbage generation hypothesis concepts through the collector Allocator

  • Weak generation hypothesis: most objects declare a short period, most objects are ephemeral

The ratio of Eden: to: from in the JVM young region is 8:1:1, which is associated with the weak generation hypothesis

  • The strong generation hypothesis: objects that survive more garbage collections are less likely to die

TLAB

Thread Local Allocation Buffer is short for an exclusive Thread based on CAS. It can allocate objects to a block of memory in Eden area first. As there is no lock contention for the memory area exclusive to Java threads, the Allocation speed is very fast.

Card Table

Each card entry corresponds to a card page. When an object reference in the card page has operations, the write barrier will mark the status of the card table where the object is. The essence of the card table is to solve the problem of cross-generation reference.

Garbage collection algorithm

It is divided into two marking algorithms and three garbage cleaning algorithms. The two marking algorithms are tag reference counting method and GC Roots Tracing, and the three garbage cleaning algorithms are mark-copy algorithm, mark-clearance algorithm and mark-compression algorithm. Different algorithms are used according to different GC.

Object allocated memory

  • Free linked list: Converts random I/O to sequential I/O by storing extra free addresses, but incurs an additional performance cost
  • Collision pointer: When a pointer is used as a demarcation point to allocate memory, it only needs to move the pointer to an idle segment equal to the size of the object. The allocation rate is high, but the usage scenarios are limited.

CMS garbage collector

CMS collector is a kind of collector, aimed to obtain the shortest recovery pauses at present a large part of the Java application on the Internet site on the service side of such applications are usually more focus on the server response speed, want the system to pause time as short as possible, the user interaction experience, good CMS collector is meeting the needs of this type of application. The whole collection process is divided into four stages:

  1. Initial tag
  2. Concurrent tags
  3. To mark
  4. Concurrent remove

STW is still needed in the two stages of initial marking and re-marking. The initial marking only marks the objects that GC Roots can be directly associated with, which is very fast. The concurrent marking phase is the process of traversing the entire object graph starting with the directly associated objects of GC Roots. The recallmarking phase corrects the marking record of the part of the object that is marked because the concurrent marking phase user program continues to operate. This pause is usually a little longer than the initial marking phase, but also much shorter than the concurrent marking phase. The final concurrent cleanup phase, which cleans and removes dead objects judged by the marker phase, can also work concurrently with the user thread because there is no need to move live objects.

GC optimization objective

Mutator

Mutator types are mainly divided into two types according to the proportion diagram of object Survival Time. Similar statements are also mentioned in weak generation hypothesis, as shown in the figure below, “Survival Time” represents the Survival Time of the object, and “Rate” represents the proportion of object allocation

  • IO interactive: Most services on the Internet, such as distributed RPC, MQ, and HTTP gateway services, are of this type. They do not require much memory. Most objects will die within TP9999
  • MEM computing: mainly distributed computing Hadoop, distributed storage, the larger the Old area, the better

Two core metrics for evaluating GC

  • Delay: also can be understood as the maximum pause time, that is, the maximum time of a STW in the garbage collection process, the shorter the better, can accept the increase of frequency to a certain extent, the main development direction of GC technology
  • Throughput: Throughput is the percentage of the total elapsed time of the Mutator system during the lifetime of the application system, as GC threads occupy the current CPU clock cycle of the Mutator. A throughput-first collector can accept longer pauses. The pause time should not exceed TP9999 of the application service, and the GC throughput should not be less than 99.99%.

Of course, in addition to the two, there are also scenarios between the two. This article mainly discusses the first case, the Surivial Time distribution map of objects, which has very important guiding significance for us to set GC parameters.

Set basic CMS parameters

In JDK8, the default collector is not a CMS
-XX:+UserConcMarkSweepGC

// Turn on the display of parameters
-XX:+PrintGCDetails
-XX:+PringtCommandLineFlags
-XX:+PrintHeapAtGC

// To output GC logs, use https://gceasy.io/ for simple and quick GC analysis
-Xloggc:D://gceasy//waterGC.log
Copy the code

Problems to prevent: dynamic expansion, system.gc (), premature promotion

1. Closed capacity expansion, fixed heap space (prevention)

When the service is just started, there will be a lot of GC and the maximum free space is large, but there will still be a lot of GC. You can observe the GC log or use the monitoring tool to observe the space change in the heap. GC Cause is usually called Allocation Failure. After a GC is observed in the GC log, the parameters in the heap are resized

// Fixed heap space
-Xms4096m
-Xmx4096m
// Set the size of the young area
-XX:NewSize=
-XX:MaxNewSize=
// The size of MetaSpace is fixed
-XX:MetaSpaceSize=
-XX:MaxMetaSpaceSize=
Copy the code

Generally speaking, the need to ensure that the Java virtual machine heap is stable, ensure * * – Xms and -xmx setting is a value, to obtain a stable heap space, in the case of not pursue the pause time shocks space is also good * *, can dynamically scale to save space, rookie year roommates said to ali cloud to cloud server costs of tens of millions, How to choose the pros and cons needs to consider the actual situation.

2. Whether system.gc () is left or not (prevention)

There are two collection modes for the Old region of CMS, which are BackGroung GC and ForeGround GC. BackGround GC is the collection behavior of CMS in the conventional sense, while ForeGround GC can trigger the whole heap GC. System.gc () triggers a ForeGround GC when called

Why does system.gc () trigger ForeGround GC

The Direct Byte Buffer, which is widely used in frameworks such as Netty, is a zero-copy area of memory that cannot be collected by a regular GC. The class sum.misc.Cleaner is needed to collect DBF garbage. In the process of collection, the Cleaner will explicitly call System.gc () to eliminate the garbage in the whole heap and prevent a certain memory area in the heap from having reference to DBF, which leads to the memory area in DBF cannot be recycled

// Lower system.gc () trigger level
-XX:+ExplictGCInvokesConcurrent
// Turn off the class unmount switch
-XX:+ExplictGCInvokesConcurrentAndUnloadClass
Copy the code

3. Premature promotion

The frequency of Full GC is relatively frequent, and the change ratio of Old area is very large after each Full GC

  • The Young area space is too small. Procedure
  • Excessive allocation rate

So as mentioned above, if the JVM instance corresponds to a RoocketMQ, a SpringCloud GateWay component, then the new generation is definitely very fast. The new generation is relatively fast. This will cause the JVM to dynamically adjust the MaxTenuringThresHold parameter of CMS. The default value of this parameter is 6. If the allocation rate of new generation objects is too high, the JVM will calculate the cumulative value of objects over the years. Then the JVM’s MaxTenuringThresHold will be adjusted to the I value, which will only cause more objects to enter the old age earlier. After entering the old age, they will soon lose their references, and GC will be triggered to collect the old age garbage. GC cannot solve the problem of object allocation rate being too fast. Therefore, garbage collection is constantly performed, resulting in the zigzag pattern of the old GCThis may seem like a simple question, but adjusting this one parameter can yield significant benefits. If the JVM instance corresponds to an interactive application, the new generation should have more space allocated than the older generation

-Xmn2560m
Copy the code

Problems that need to be solved

MetaSpace OOM

As mentioned above, MetaSpace mainly stores some Class meta-information, and the life cycle of the data is the same as the life cycle of the Class loader. The key cause of this problem is that the ClassLoader keeps loading new classes in memory. This problem usually occurs in the dynamic Class loading problem. The most common problems are still concentrated in reflection, JavaSisit bytecode enhancement, CGLIB dynamic proxy and other technical points. In addition, it is necessary to add a monitor to the utilization rate of MetaSpace area immediately. If the indicators fluctuate, the problem needs to be detected and solved in advance

CMS GC happens too often

The time of CMS GC occurring frequently in the Old area is not special, and the overall maximum STW is also within the acceptable range. However, it is common that GC is too frequent, resulting in fast throughput decline. Responsible for dealing with a background of CMS GC thread concurrentMarkSweepThread constantly polling, use shouldConcurrentCollect () method to do a test, judging whether the Old GC is required

  • If open – XX: UseCMSInitiatingOccupancyOnly parameters, judge whether the current utilization of Old area is greater than the threshold, if is greater than the threshold, triggering a CMS GC, if not set the default value is 92%
  • If the previous Young GC failed, or the next Young GC may fail, the CMS GC needs to be triggered

This problem is usually caused by a memory leak, and frequent GC is still not able to reduce the memory usage to a low level, in which case a memory leak analysis is required.

  • Memory Dump is performed before and after the CMS GC
  • Analyze the Top Component, viewing the histogram by object, class, classloader, etc., and analyze the associated objects using outgoing and incoming
  • Unreachable object

CMS Old GC takes too long

The CMS GC word STW has a maximum timeout of 1000ms and does not occur frequently, as shown in the figure below. The maximum timeout is 8000ms, which can cause “avalanche effect” in certain scenarios. This scenario is very dangerous and should be avoided as much as possibleFocus on theInitial tagandIn the end tagThe two stages of

The problem is usually with the final tag

The initial stage of Final Remark is the same as the process of Init Mark, but the subsequent Card Table traversal, Reference instance cleaning and adding it to the Pend_list of Reference maintenance, if metadata information is to be collected, Also clean up resources that are no longer used in SystemDictionary, CodeCache, SymbolTable, StringTable, and so on

The analysis of FinalReference mainly observed the Dominator tree of java.lang.ref.Finalizer object to find the source of the leak. Socket SocksSocketImpl, Jersey ClientRuntime, Mysql ConnectionImpl, etc. In addition, jdK8 will enable CMSClassUnloadingEnabled, which will cause the CMS-remark phase to attempt to unload classes. Turn on -xx: -cmsClassUnloadingEnabled to avoid the unloading process of MetaSpace classes

conclusion

There are four steps to JVM optimization

  1. For an in-depth understanding of the Java Virtual Machine, you must read this book, master the basic knowledge of GC, basic JVM analysis tools
  2. Understand basic GC evaluation methods, how to set metrics for individual systems, and how to determine if GC is a problem in a business scenario
  3. Perform actual scene optimization
  4. Repeat the process over and over again