This is the 18th day of my participation in the August More Text Challenge.

In what part of the JVM does GC occur, there are several GCS, and what are their algorithms?

Brain figure:

Which part of the JVM does GC occur

GC occurs in the JVM heap

JVM architecture overview

How many GC’s are there?

Two types of GC

  • Young Minor GC
  • Old age Full GC
  • There is no GC in Perm area

What is the algorithm for GC

Four kinds of algorithms

1. Reference counting (Java deprecated)

Add a reference counter to the object that increments its value every time it is referenced somewhere; When a reference is invalid, the counter value is reduced by 1;

Objects with a counter value of 0 are no longer used, and the garbage collector will reclaim the object and place it in the == old age == if it is used frequently.

Features: Fast speed. But “cross-reference, circular reference” objects cannot be reclaimed. (This algorithm is no longer used because of this flaw.)

2. Copying algorithms

It divides the available memory into two pieces, uses only one piece at a time, and when this piece is used up, copies the remaining objects onto the other piece, and then cleans up the used memory all at once.

In this way, only half of the region is reclaimed each time, and there is no need to consider the complexity of memory fragmentation when allocating memory, just move the pointer and allocate it in order.

Cons: Half the total available memory, which is too expensive

All current commercial virtual machines use this algorithm to reclaim the new generation. The memory of the new generation is divided into a large Eden space and two smaller Survivor Spaces, and Eden and one Survivor are used each time.

At each collection, the surviving objects in Eden and Survivor are copied to another Survivor space at a time, and Eden and the Survivor space that was just used are cleaned up. When Survivor space runs out, you need to rely on older generations for allocation guarantees (Handle Promotion).

Features:

  • == Younger generation ==
  • High efficiency, no memory fragmentation, take up large space

The execution process of the replication algorithm is shown as follows:

3. Mark-sweep

It is divided into “mark” and “clear” two stages: first, mark all the objects to be cleared, and clear all the marked objects after marking. The deficiencies are mainly reflected in efficiency and space: from the perspective of efficiency, the efficiency of both marking and clearing is not high; From the perspective of space, a large number of discrete memory fragments will be generated after mark clearing. Too many memory fragments may lead to the failure to find sufficient contiguous memory during the later program running when large objects need to be allocated and have to trigger a garbage collection action in advance.

Features:

  • Inefficient, fragmented
  • == old age ==

The execution process of mark-clear algorithm is shown as follows:

4. Mark-compact algorithm

The replication algorithm has low efficiency because it needs to perform a large number of replication operations when the object survival rate is high. In case the object is 100% alive, there needs to be additional space for allocation guarantees. The old age is not easy to be recycled object, object survival rate is high, so generally can not directly choose the replication algorithm. In the old mark-clean algorithm, the process is the same as the mark-clean algorithm, but instead of cleaning up the recyclable objects directly, all living objects are moved to one end and the memory beyond the boundary is cleaned up directly.

Features:

  • == old age ==
  • Need to move fragmentation, inefficient, no memory fragmentation

The working process of mark-collation algorithm is shown as follows:

5. Generational collection algorithm

Modern commercial virtual machines (VMS) basically use generational collection algorithms for garbage collection. This algorithm is just a combination of these,

According to the different life cycle of the object, the memory is divided into several blocks, and the most appropriate collection algorithm is adopted according to the characteristics of each block.

A large number of objects die and a small number of objects survive (the new generation), using the replication algorithm, low replication cost;

For those with high survival rate and no extra space for allocation (old age), mark-clean algorithm or mark-tidy algorithm is adopted.