JVM garbage collection algorithm summary

JVM garbage collection algorithms are used mainly in the following three:

  • Mark - sweep algorithm
  • Mark - Collation Algorithm
  • Replication algorithm

Let's look at one by one.

 

First, mark - sweep algorithm

As its name suggests, the algorithm is divided into "mark" and "clear" in two stages:

  • Mark phase : Mark all the objects need to be recovered. (Methods marked GC Roots reachability analysis algorithm)
  • Clear stage : just the unity of all the objects are marked recovery.

Illustrated as follows:
Here Insert Picture Description
The algorithm has two major drawbacks:

  • Marking and clearance process efficiency is not high
  • Will produce a large number of discrete memory fragmentation, leading to the large object can not allocate memory to allocate memory for large objects, then the GC will trigger advance

 

Second, the mark - Collation Algorithm

Mark phase with the mark - sweep algorithm as mark - finishing the second stage is to move all live objects are moved to the end, then clean out the memory directly outside the terminal boundary.
Here Insert Picture Description
Clearly mark - finishing solve the mark - sweep algorithm of memory fragmentation issues.

But it also has a disadvantage that need to move large numbers of objects, the processing efficiency is relatively low.

 

Third, replication algorithm

In order to solve the efficiency problem, collect algorithm called "copy" appeared, it would equal the size of the memory is divided into two, each only use one, when this one runs out of memory objects will also be copied to survive another piece of the above, and then used to conduct a clean-up memory space.

As shown below:
Here Insert Picture Description

The disadvantage is also very obvious ah. . . That is only half the memory, wasting a lot of memory space.

Now commercial virtual machines are using this new generation of recycling collection algorithm, but is not divided into two equal size, but a large space of Eden and two Survivor smaller space, Eden and which one each use Survivor. When recycling, the Eden and Survivor also alive objects copied to another piece of Survivor, final clean-up Eden and used it a Survivor.

Eden and Survivor size ratio HotSpot virtual machine defaults to 8: 1, to ensure that the memory utilization rate of 90%. If every object is more than 10% recovery of survival, then a Survivor is not good enough, then you need to rely on old's spatial distribution guarantee, which is stored in the object space does not fit the old era borrowing.

 

Fourth, generational collection algorithm

Now commercial virtual machine using generational collection algorithm, this algorithm is not new, but is a combination of boxing, it divides memory into several pieces, different block using appropriate collection algorithm based on the object survival period.

General dividing the heap into the new generation and the old era.

  • Cenozoic use: replication algorithm
  • Old's use: mark - sweep or mark - Collation Algorithm

Guess you like

Origin blog.csdn.net/u013568373/article/details/94352202