Garbage collector (GC)

1 Overview

In various parts of the Java run-time memory, the program counter, stack virtual machine, native method stacks with three regions born threads, with threads off; stack as the stack frame of the entry and exit method for the orderly execution and stack pop operations. How much memory is allocated for each stack frame is substantially determined when the class structure down to known, memory allocation and recovery of these regions are provided with certainty, it is in these areas does not require consideration recovered question, because the method ends or when the end of the thread, just as the natural recovery of the memory. The Java heap and method area is not the same, more of a class needs to implement memory interfaces may be different, multiple branches need a method of memory may not be the same, we are only in the program in order to know when during operation which objects will be created, which is part of the memory allocation and recovery are dynamic garbage collector concern is this memory.

Conclusion: The garbage collector is a major collection of Java heap and method area

2 to determine whether the object is alive

Determine whether an object has survived, there are two ways

  • Reference counting
  • Reachability analysis

2.1 Reference counting

When an object is referenced by other objects A B, the object A reference +1 or -1 reference to disconnect, when GC work, checks the reference count of all objects, if set to 0 to be cleared,> 0 indicates there other object references can not be cleared.

Advantages: simple high efficiency
disadvantages: each circular reference problem can not be resolved between the object

Java, has not been using this method.

2.2 reachability analysis

In the commercial mainstream programming languages ​​(Java, C #, etc.) mainstream implementations are by reachability analysis to determine whether the object alive. The basic idea of ​​the algorithm is: Through a series called "GC Roots" objects as a starting point, to start the search down from these nodes, called search path traversed chain of references, when an object to GC Roots do not have any when connected to a reference chain (graph theory, then, is from GC Roots to this object unreachable), then it proves that this target is not available. Figure 2.1, object5, object6 object7 and although there are interrelated, but they are not up to the GC Roots, they would be determined to be recyclable objects.

Here Insert Picture Description

2.1 reachability analysis example of Fig.

In the Java language, can be used as an object GC Roots include the following categories

  • VM stack (local variables table stack) referenced objects
  • Method static property class object referenced area.
  • The method of constant reference object region
  • Native method stacks in the JNI (i.e., the general said method Native) object reference.

Even unreachable in reachability analysis algorithm object, will not be recovered, the recovery is real need at least twice the labeling process: If the object reachability analysis found that the objects between GC Roots and no chain of references, marking the first time it will be followed by a screening. Screening method is whether the object is necessary to perform a finalize () method.

  • If the object does not cover the finalize () method, or a virtual machine has been called finalize () method, it indicates that the object is not necessary to perform the finalize () method, and the object will be directly recycled.
  • If the object covers the finalize () method, and the object is not executed finalize () method, then the F-Queue object into the queue, finalize () method performs the object queue by a low priority thread. After machining, GC will determine whether the object can reach, if not up to the recovery again. Otherwise, the object "resurrection."

3 garbage collection algorithm

Garbage collection algorithm mainly includes the following four

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

3.1 mark - sweep algorithm

Clear labeling algorithm is divided into "mark" and "clear" two stages: first mark all objects that need to be recycled, recovered all unified object is marked in the mark after completion. This algorithm is the most basic algorithm, the follow-up of this idea and algorithms are improved algorithm for its shortcomings. This lack of local algorithms are:

  • Efficiency, marking and removal efficiency of both processes is not high
  • Space problem, mark - will produce a large number of discrete memory after clearing the debris, if too much memory fragmentation, when the follow-up procedure needs to allocate a relatively large memory space may not be able to find a continuous and sufficient memory space.

Clear execution flag algorithm shown in Figure 3.1:
Here Insert Picture Description

FIG labeled 3.1 - Clear schematic Algorithm

3.2 replication algorithm

In order to address efficiency issues raised replication algorithm. The main idea is to copy algorithm: by available memory capacity is divided into two equal size, uses only one of which, when run out of memory where a copy of the object will still alive one above the other, and then the first memory space has been used to clean out.

Here Insert Picture Description

FIG schematic 3.2 replication algorithm

This algorithm is generally used to recover the new generation, it also has shortcomings and inadequacies.

  • At higher survival rate target will be more copy operations, reducing the efficiency
  • If the object is alive too, a reserved area can not copy all live objects, you will need additional space allocation guarantee.

Allocation guarantees example:

The company specializes in IBM showed that 98% of the objects in the new generation are "raw evening toward death", so do not need a 1: 1 ratio to divide the memory space, but the memory is divided into a space and two dozen of Eden Survivor block smaller space, and which each use a Survivor Eden. When recycling, the Eden and Survivor also live objects copied to another piece of disposable Survivor space, and finally clean out Survivor Eden and just used space. HotSpot VM Eden and Survivor default size ratio is 8: 1, i.e. each new generation the available memory space for the new generation of 90% of the whole capacity (80% + 10%), only 10% of the memory is "wasted . " Of course, only 98% of the recoverable data objects in a scene in general, we can not guarantee that every recovery are only more than 10% of live objects, when Survivor space is not enough, need to rely on other memory (referred to herein as old years) were allocation guarantee.

3.3 mark - Collation Algorithm

According to the characteristics of old age, it has been proposed mark - sorting algorithms. The algorithm is also survived an object moves to one side, then clean out the memory beyond the boundary.

Here Insert Picture Description

FIG labeled 3.3 - Finishing Algorithm schematic

3.4 generational collection algorithm

The algorithm is based on different objects alive the memory cycle is divided into a few pieces. The Java heap is generally divided into the old and the new generation's, so you can use the most appropriate collection method based on the characteristics of each era. In the new generation, each time garbage collection will find a large number of dead objects, only a few survive, then copy the selection algorithm, only need to pay a small amount of live objects replication costs to complete the collection. The old era because of the high survival rate of the object, there is no guarantee additional space to be allocated, you must use the mark - to clean up or mark - Collation Algorithm for recycling.

Published 107 original articles · won praise 142 · views 130 000 +

Guess you like

Origin blog.csdn.net/u013293125/article/details/93173499