JVM Series II (garbage collection algorithm).

First, mark - sweep algorithm (Mark-Sweep)

This algorithm is divided into "mark" and "clear" two stages: first mark all objects need to be recovered, all unified object is marked recovery after labeling is complete.

Mark-Sweep algorithm is the most basic collection algorithm, almost all of the collection algorithms are based on this idea and make improvements to its lack of give. It has two major shortcomings:

  • Efficiency. Mark and sweep efficiency of the two processes are not high;
  • Space. It will produce large amounts of memory fragmentation mark after clearing space debris could cause too much when the need to allocate large objects, can not find enough contiguous memory and had to trigger another garbage collection operation in advance.

Second, the replication algorithm (Copying)

This algorithm by available memory capacity is divided into two equal size, uses only one of them. When this one runs out of memory, it will mark surviving copy of an object to another one above, and then half-area space and then used a one-time clean out.

Copying algorithm for each area only half recovered, a good solution to the problem of memory fragmentation, and the simple, efficient operation.

The shortcomings of this algorithm is:

  • The memory is reduced to half the original, costly and requires additional space to do assignment guarantee.
  • More copy operations will be carried out at a higher survival rate of the object, reducing efficiency.

Now commercial virtual machines are using this collection algorithm to recover the new generation.

Third, the mark - Collation Algorithm (Mark-Compact)

This algorithm is divided into "mark" and "finishing" two stages: first mark all objects need to be recovered, and let all live objects are moved to the end, then clean out the memory directly outside the terminal boundary.

Mark-Compact algorithm can solve memory fragmentation, to avoid the waste of space and efficiency Copying algorithm.

Fourth, generational collection algorithm (Generational Collection)

This algorithm is based on different objects alive the memory cycle is divided into a few pieces, usually the Java heap into the new generation and the old generation.

In the new generation, we have found each garbage collection when there are a large number of dead objects, only a few survive, then choose Copying algorithm, only to pay a small amount of live objects replication costs to complete the collection.

In the old generation, high survival rate target, there is no extra space for it to be secured, it is necessary to use the Mark-Sweep or Mark-Compact algorithm.

Guess you like

Origin www.cnblogs.com/jmcui/p/12044343.html