Three algorithms of JVM garbage collection (5)

Mark-clear algorithm (most basic)

The algorithm is divided into two stages: "marking" and "clearing". First, mark all the objects that need to be recycled. After the marking is completed, all marked objects are uniformly recycled, or the surviving objects can be marked, and unmarked objects are uniformly recycled. Object.
Disadvantages:

  1. The execution efficiency is unstable. If the Java heap contains a large number of objects, and most of them need to be recycled, a large number of marking and cleaning actions must be performed, resulting in the execution efficiency of both marking and cleaning processes decreasing with the increase of objects.
  2. Memory fragmentation, marking, and clearing will produce a large number of discontinuous memory fragments. Excessive space fragmentation will cause the program to allocate a larger space when it is impossible to find enough continuous memory space and have to trigger a garbage collection in advance. .

Mark-copy

Solve the problem of low execution efficiency of the mark-sweep method facing a large number of recovered objects.
**Method:** Partition the reclaimed area, some of which store objects, and the remaining areas are reserved. When reclaiming, the surviving objects are allocated to the reserved partitions, and the used partitions are cleaned up at once.
Don't worry about memory fragmentation during the allocation process, just move the pointer at the top of the heap and allocate in order.
**Pros: **Simple and efficient
** Disadvantages:**The reserved partition should not be too large, otherwise it will cause a waste of space.
Appel-style recycling: The new-generation collectors such as Serial and ParNew in the HotSpot virtual machine use this strategy to design the new-generation memory layout.
Method: Divide the new generation into a larger space Eden and two smaller Survivor spaces. Only Eden and a piece of Survivor are used for each allocation. When garbage collection occurs, the surviving objects are allocated to another Survivor space, and Eden and the used Survivor space are cleaned up.
The HotSpot virtual machine defaults to the size ratio of Eden and Survivor of 8:1. Eden is 80% and Survivor is 10%. It may not be enough to store surviving objects with 10% (the probability is rare), so Appel-style recycling has designed an "escape door". When Survivor airborne is insufficient to accommodate a Minor GC, the surviving objects are , You need to rely on other memory areas (mostly old generations) for allocation.

Mark-up algorithm

Use the mark-sort algorithm to reclaim the old generation. The marking process is the same as the mark-sweep algorithm, but the difference is that the mark-sweep algorithm does not directly clean up recyclable objects. Instead, all surviving objects are moved to one end of the memory space and then cleaned up directly. Memory outside the boundary.
The mark-sweep algorithm is essentially different from the mark-sort algorithm; the former is a non-mobile recycling algorithm, and the latter is mobile.
The moving object must suspend the user's application all the way to proceed, and the operation is extremely heavy. Not moving the object will cause memory fragmentation. If it is solved through the "partition free allocation linked list", it will directly affect the throughput of the program. There are drawbacks to moving and not moving.
Non-moving memory allocation will be more complicated, and moving memory recycling will be more complicated. From the perspective of garbage collection time, the pause time for not moving objects will be shorter, and there is no need to pause, but from the throughput of the entire program, moving objects is more cost-effective.
The Parallel Scavenge collector that focuses on throughput in the HotSpot virtual machine is based on the mark-sweep method, while the low-latency CMS collector is based on the mark-sweep method.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109260406