JVM Collection Algorithms Garbage Collector Metaspace References


JVM collection algorithm

Earlier we learned that the entire heap memory is actually based on the generational collection mechanism, but we still haven’t talked about how it is implemented specifically, so how is the specific process implemented? let's find out

mark-sweep algorithm

insert image description here
This recycling method is to first mark the objects that need to be recycled, and then recycle the marked objects one by one, or mark all the objects that do not need to be recycled, and only recycle the unmarked objects. Although this method is very simple, its disadvantages are also
very Obviously, first of all, if there are a large number of objects in the memory, there may be a large number of marks and large-scale cleaning. And after a mark is cleared, there may be many gaps in the continuous memory space, and fragmentation will reduce the utilization rate of the continuous memory space.

mark-copy algorithm

insert image description here
Our marking algorithm is to divide the heap area into two areas of the same size and put them into areas, and then only use one of the areas each time. Whenever it comes to garbage collection, mark the objects that need to be recycled, and then copy the unmarked objects to On the other side, the current area will be emptied at the last time. Although copying wastes some time, it can solve the fragmentation problem caused by large-scale recycling of objects. This algorithm is very suitable for the new generation (because the new generation is recycled
) The efficiency is extremely high, generally not leaving too many objects) garbage collection, and the new generation Survivor area we mentioned before is actually this idea, including the ratio of 8:1:1 is also for the mark copy algorithm taken for optimization.

Mark-Collating Algorithm

insert image description here
We mentioned the replication algorithm above. It is no problem to apply this algorithm completely in the new area, but if it is used in the elderly area, it will be very tasteless, because the elderly area is basically a number of nail households. It is not like the new area that will be recycled every time Only by freeing up a lot of space, objects can have a chance to enter the old generation, so the old generation is generally a nail household, and there may still be many objects left after a GC. The mark copy algorithm will completely copy the contents of the entire area after GC, and will damage 50% of the area, which obviously does not apply to the old generation.
So can we do this, after marking all the objects to be recycled, we don’t rush to perform the recycling operation, but arrange all the objects to be recycled neatly in a memory space, and throw all the objects that need to be recycled back, so that, All objects in the first half do not need to be recycled, while the second half can be cleared at once.
Although this can ensure that the memory space is fully used, and it is not as complicated as the mark copy algorithm, its disadvantages are also obvious, and its efficiency is lower than the former two. Even, because the position of the object in memory needs to be modified, the program must be paused at this time. In extreme cases, the entire program may be paused. Therefore,
we can mix the mark-clearing algorithm and the mark-clearing algorithm. When the space is not very messy, it is not a big problem to use the mark clearing algorithm. When the memory space is messy to a certain extent, we can perform a mark clearing algorithm.


JVM garbage collector

Serial collector

This collector is a relatively old collector. In the earlier jdk, it was the only choice for the new generation area collector of the virtual machine. This is a single-threaded garbage collector, that is to say, when garbage collection starts At this time, it is necessary to suspend all threads until the end of garbage collection. His new generation collection algorithm uses the mark copy method, and the old generation uses the mark collation method
insert image description here

ParNew collector

This garbage collector is equivalent to a multi-threaded version of the Serial collector, which can support multi-threaded garbage collection:
insert image description here
except for multi-thread support, other content is basically the same as the Seria collector, and some JVM default server mode new generation The collector is the ParNew collector used.

Parallel Scavenge /Parallel Old Collector

Parallel Scavenge is also a garbage collector for the new generation. It is also implemented using the mark copy algorithm. In JDK6, its old generation collector Parallel Old was also launched, which is implemented using the mark sorting algorithm: unlike the ParNew collector,
insert image description here
it It will automatically measure a throughput and determine the time of each garbage collection according to the throughput. This adaptive mechanism can well weigh the performance of the current machine and choose the optimal solution according to the performance.
At present, JDK8 adopts the garbage collection scheme of Parallel Scavenge + Parallel Old.

CMS collector

In JDK1.5, HotSpot launched a garbage collector that can be considered as an epoch-making significance in strong interactive applications: CMS (Concurrent-Mark-Sweep) collector, this collector is the first real garbage collector in the HotSpot virtual machine Concurrency in the sense (note that there is a difference between concurrency here and the previous parallelism, concurrency can be understood as running user threads and GC threads at the same time, and parallelism can be understood as multiple GC threads working at the same time) collector, it is the first time to implement In order to allow the garbage collection thread to work concurrently with the user thread.
It mainly uses the mark-and-sweep algorithm:

insert image description here

Garbage First (G1) collector

We know that our garbage collection is divided into Minor GC, Major GC and Full GC, which respectively correspond to the garbage collection of the new generation, the old generation and the entire heap memory, and the G1 collector cleverly bypasses these conventions, it will The entire Java heap is divided into 2048 independent Region blocks of the same size. The size of each Region block is determined according to the actual size of the heap space. The overall size is controlled between 1MB and 32MB, and both are 2 to the Nth power. All Regions are the same size and will not change throughout the lifetime of the JVM.
So what's the point of separating these Regions? Each Region can freely decide which role to play (Eden, Survivor, and Old Generation) according to its needs. The collector will adopt different recycling strategies according to the corresponding roles. In addition, the G1 collector also There is a Humongous area, which is specially used to store large objects (generally, objects whose size exceeds half of the Region capacity are considered large objects). In this way, the new generation and the old generation are no longer a continuous memory area physically, but are everywhere Distributed.

insert image description here
Its recovery process is roughly similar to that of CMS:
insert image description here
Initial mark: Mark the objects that the object can be associated with
Concurrent mark: Through reachability analysis, recurse the object graph in the entire heap to find out the objects to be recycled
Final mark: Yes The user thread makes a short pause to process the part of the object that was missed during the concurrent marking phase.
Screening and recycling: making a recycling plan


Metaspace

insert image description here
After JDK8, there is no previous permanent generation in our heap, and a thing called metaspace is created. The meta information of the class is stored in the metaspace. The metaspace does not use heap memory. Theoretically, the system can use As much memory as there is, there is as much metaspace as there is. There will be no memory overflow problem when the permanent generation exists, and the permanent generation will be completely abandoned.
insert image description here


quote

strong reference

In Java, if a variable is an object type, it actually stores a reference to the object. A reference type like Object o = new Object() is a strong reference. We can make it clear from the previous study that if there is such a method in the
method The strong reference type, now need to reclaim the object pointed by the strong reference, then either this method ends, or the reference connection is disconnected, otherwise the referenced object cannot be judged as recyclable, because we may use it later it.
Therefore, when the JVM memory space is insufficient, the JVM would rather throw an OutOfMlemoryError to cause the program to terminate abnormally, and would not solve the problem of insufficient memory by arbitrarily recycling "survival" objects with strong references.
Strong reference writing: Object o = new Object();

soft reference

Soft references are not recyclable like strong references, but once the JVM runs out of memory, it will ensure that the object pointed to by the soft reference is cleaned up before an exception is thrown. Soft reference writing
: SoftReference reference = new SoftReference<>(new Object()) ;

weak quotation

The life cycle of a weak reference is shorter than that of a soft reference. During garbage collection, its memory will be reclaimed regardless of whether the current memory is sufficient WeakReference
reference
= new WeakReference<>(new Object());

phantom reference

It may be recycled at any time
, which means that no matter how many times we call the get() method, we will always get null, because the phantom reference itself is not a reference, which means that this object does not have any references, and we can only use queued ones. Constructor to be notified when the object is recycled.

Guess you like

Origin blog.csdn.net/m0_62434717/article/details/129723140