JVM garbage collection and jvm garbage collection
JVM garbage collection
This article attempts to briefly introduce the mainstream JVM garbage collection mechanism.
Scope of a garbage collection Discussion
JVM memory is divided into several parts: Method Area, JVM stack, local method stack, heap, program counter, etc.
The life cycle of the program counters, JVM stacks, and local method Stacks is the same as that of the thread. With the thread reclaim, the life cycle of these memories naturally ends and can be recycled.
The method area is used to store static variables, constants, class information, and other data. The heap is used to divide the memory on the object when it is created. As the program runs, the data will no longer be useful at any time and need to be recycled is a concern of garbage collection.
IIBasic garbage collection policies
The problem to be solved in garbage collection is to recycle useless objects in the memory, so as to release the memory space for subsequent program execution to allocate space. The main problems here are:
When Will objects be useless?
In other words, it is how to judge that the object will not be used by any program. One solution is the so-called reference counting method, that is, when every part of the program references this object, the reference technician + 1. When each reference fails, corresponding to-1, when the "reference counter" of an object is set to zero, it is assumed that it can be recycled. This solution seems to be effective, but A potential problem is circular reference. Assume that object A holds A reference of object B, and object B holds A reference of object A at the same time, when there is no reference pointing to the two objects in the program elsewhere, A and B still hold each other, which causes A and B to never be recycled, imagine that there are multiple similar phenomena in the program, which can easily lead to memory overflow.
In mainstream programming languages, accessibility Analysis is usually used to determine whether an object is useless. Accessibility analysis refers to the search from GC Roots. If an object does not have any GC Roots, it is considered no longer useful. The above-mentioned Mutual references are obviously inaccessible from GCRoots. In Java, possible GC Roots objects include:
Objects referenced in the JVM Stack
Objects referenced by static attributes in the Method Area
Objects referenced by constants in the method Area
Objects referenced in the local method Stack
How can we make recycling more efficient?
The so-called efficient problem can be understood as how to achieve the most garbage collection with the least impact on the application. That is to say, garbage collection should not affect the normal operation of the program as much as possible, and efficiently recycle objects that are no longer useful to provide sufficient space for subsequent operation of the program.
Three garbage collectors
1.1. Serial/Serial Old collector
As the name suggests, this is a single-thread collector. When the thread is recycled, there is only one thread, and all user threads will be stopped, that is, Stop the world. Obviously, for server programs that require high performance, this is unacceptable. For a lightweight client program, if the Stop the world is only in milliseconds or tens of milliseconds, it is not easy to use this simple and efficient garbage collector.
1.2. ParNew collector
The ParaNew collector can be considered as a multi-threaded version of the Serial collector and is a new generation of garbage collector. Since the Parallel Scavenge collector cannot be used with the CMS collector, the ParNew collector becomes the first choice for the new generation of collectors used with the CMS. When the CMS collector is used, the ParNew collector is the default New Generation collector. You can also use-XX: + UseParNewGC to forcibly specify to use this collector.
1.3. Prarallel Scavenge collector
This collector is also a new generation of multi-thread collectors. The main difference from the ParNew collector is that the collector focuses on the throughput parameter.
Throughput = time-of-user-program/(time-of-user-program + time-of-GC)
The higher the throughput, the higher the CPU utilization of the system. The higher the throughput, the more time can be invested in actual computing. This is suitable for systems that require a large amount of computing at the backend and have less interaction.
The collector provides two parameters-XX: MaxGCPauseMillis and-XX: GCTimeRatio to control throughput parameters. Throughput = 1/(1 + GCTimeRatio ). The-XX: + UseAdaptiveSizePolicy is also provided to enable adaptive adjustment of the size of each memory partition.
1.4. Parallel Old collector
The Old version of the Parallel Scavenge collector when Parallel Old collector is used in scenarios with limited throughput.
1.5. CMS (Concurrent Mark Sweep) Collector
The collector uses the Mark-clearing algorithm to target the shortest pause time. The recycling process can be divided into four steps:
A. initial mark
Mark GC Roots can be directly associated to reach the object, the speed is very fast, you need to Stop the world
B. mark concurrent mark for concurrency
Perform GC Roots Tracing, which can be performed together with the user thread
C. remark the remark.
Modify the part of the tag that changes when the user program runs during the concurrent tag. Run the script in parallel and stop the world.
D. Clear concurrent seep concurrently
Run together with the user thread
During the whole process, 1 and 3 need to stop the world, but the Occupied time is short, while 2 and 4 occupy a long time, but can be performed together with the user thread, therefore, the overall pause effect on user programs is small.
1.6. G1 (Garbage First) Collector
The G1 collector can independently recycle garbage from the new generation and old generation. In addition to the traditional division, it divides the memory area into several independent Region. In general, G1 adopts the tag-sorting algorithm, while each Region adopts the replication algorithm, which effectively avoids frequent GC caused by a large amount of memory fragments. In addition, G1 evaluates the value of each Region during each collection, and preferentially recycles the high value Region. Mainly divided into the following steps
Other steps are similar to CMS, while step 4 mainly evaluates the size and cost of each Region recovery value, and recycles the value at the user's expected cost.