JVM tuning series: garbage collection and jvm Tuning
Summary: jvm is a required series, which summarizes some common jvm collection mechanisms for easy reference.
Before optimization, we must understand how it runs. java's Garbage Collection is usually called "GC", which was born in The Lisp Language of MIT in 1960, after more than half a century, it has become very mature. Therefore, this article focuses on these three aspects:
1. What objects need to be recycled?
2. When will it be recycled?
3. How to recycle it?
Who will be recycled
The java Virtual Machine divides the memory managed by the java program into several different data regions, which have their respective purposes. It consists of the following parts:
Program counters
We can ignore the memory space occupied by the program counter. It is the row number indicator of the bytecode executed by each thread.
Virtual Machine Stack
Java Virtual Machine stacks are private threads, with the same lifecycle as threads. It describes the Memory Model of method execution. It is also used to store local variables, operand stacks, dynamic links, and method exits.
Local method Stack
The local method stack, similar to the Virtual Machine stack, calls the native method.
Heap
Heap is the largest memory in the jvm. It is shared and stores object instances. It is also called "gc Heap ". Main management area of garbage collection
Method Area
The method area is also the shared memory area. It mainly stores information about classes loaded by virtual machines, constants, static variables, and code data compiled by the real-time Compiler (jit.
The above is the main memory composition of jvm during runtime. We can see that common memory usage not only exists in the heap, but also exists in other regions, although heap management is crucial to program management, we cannot just limit it to this region. Especially when memory leaks occur, apart from checking heap memory, the Virtual Machine stack and the method area must be considered.
Now that you know who you want to perform memory management for and those regions, I also need to know when to recycle these regions.
When to recycle
Before garbage collection, we must determine whether the object is alive? This involves the algorithm for determining whether an object is alive.
Reference Counting Algorithm
Add a reference counter to the object. Every time a counter is referenced, counter + 1. When the reference is invalid, counter-1. objects whose counter is 0 at any time cannot be used again.
Advantages: simple implementation, efficient determination, and widely used in actionscript3 and python.
Disadvantage: the problem of mutual reference between objects cannot be solved. Java is not adopted
Accessibility Analysis Algorithm
Using a series of objects called "GC Roots" as the starting point, you can start searching down from these nodes, and the path you search for is called a reference chain, when an object to GCRoots is not connected by any reference chain, it is proved that this object is unavailable.
For example, if the object on the right is inaccessible to GCRoot, it can be determined to be a recyclable object.
In java, GCRoot objects include the following:
* Objects referenced in the VM stack.
* Objects referenced by static attributes in the method area.
* Objects referenced by constants in the method area.
* Objects referenced by JNI in the local method.
Based on the above, we can know that when the current object cannot be reached in GCRoot, it will meet the possibility of garbage collection.
So are these objects not dead, not necessarily? At this time, they can only be declared in a "probation" stage, to truly declare the death of an object. At least two tags are required:
The first time: After the object accessibility analysis, it is found that it is not connected to GCRoots and will be marked and filtered for the first time.
Second time: the object does not overwrite the finalize () method, or the finalize () method has been called by the Virtual Machine. Therefore, the finalize () method is considered unnecessary.
How to recycle
After the above two points are explained, we will probably understand which objects will be recycled and the basis for recycling. However, the implementation of recycling is not simple.
First, it needs to scan all objects to identify who can be recycled. Second, during the scan, it needs to "stop the world" to freeze the object. Otherwise, your reference information has changed, you do it in white.
Generational recovery
We use object1 to describe the collection path in the generational garbage collection algorithm.
1. Newly created object1, born in the Eden region of the new generation.
2. minor GC and object1 are still alive and moved to the Fromsuvivor space, which is still in the new generation.
3. minor GC and object1 are still alive. In this case, the object1 will be moved to the ToSuv area through the replication algorithm. In this case, the age of object1 is + 1.
4. minor GC and object1 are still alive. At this time, objects of the same age as objective VOR and object1 do not reach half of objective vor. Therefore, the fromSuv and Tosuv regions are swapped through the replication algorithm, the surviving object is moved to Tosuv.
5. minor GC and object1 are still alive. At this time, objects of the same age as objective VOR and object1 have reached more than half of objective VOR (the area of toSuv is full), and object1 has been moved to the old area.
6. After object1 remains alive for a period of time, it is found that object1 cannot reach GcRoots at this time, and the space ratio of the old age has exceeded the threshold, triggering majorGC (it can also be considered fullGC, but you need to contact the Garbage Collector). In this case, object1 is recycled. FullGC triggers stop the world.
In the new generation above, we have mentioned the age of objects. objects that survive in the VOR State will not be immediately promoted to the old generation, so as to avoid a huge impact on the old generation, they must meet the following conditions before they can be promoted:
1. After minor gc, the age of the objects that survive in the region vor will be + 1. When the value exceeds (default) 15, it will be transferred to the old age.
2. Dynamic Objects. If the size of all objects of the same age in the primary vor space is more than half the size of the primary vor space, objects of a grade greater than or equal to the primary vor space can directly enter the old age.
The above uses the idea of generational garbage collection, the history of an object from survival to death. During this period, the replication algorithm will be used in the new generation. In the old age, the mark-clear algorithm or the mark-sort algorithm may be used, these are the implementation of garbage collection algorithms based on different regions. Let's take a look at the implementation principles of these algorithms.
Garbage collection Algorithm
Mark-Sweep)
The Mark clearing method is the ideological basis of the garbage collection algorithm. The tag clearing algorithm divides garbage into two phases: the tag phase and the clear phase.
During the marking stage, all reachable objects starting from the root node are marked through the root node. Unlabeled objects are unreferenced spam objects.
Purge stage to clear all unlabeled objects.
Copying)
The replication algorithm is to divide the original memory space into two blocks and use only one of them at a time. During garbage collection, the surviving objects in the applicable memory are copied to unused memory blocks, then clear all objects in the memory block used.
Mark-Compact)
The Mark compression algorithm is a collection algorithm in the old age.
The markup phase is consistent with the markup clearing algorithm, marking reachable objects once.
In the cleaning phase, all surviving objects are compressed to one end of the memory to avoid Memory fragments.
Garbage Collector
The garbage collector is the specific implementation of memory recycling. The garbage collectors provided by different vendors are very different. Generally, the garbage collectors act on different generations and need to be used together. The following is a combination of various garbage collectors:
Advantages and disadvantages of various combinations:
We recommend a communication and learning group: 650385180, which will share some video recordings recorded by senior Architects: Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed, and microservice architecture principles, JVM performance optimization is a required knowledge system for architects. You can also get free learning resources, which has benefited a lot: