Java interview questions of the Java Virtual Machine garbage collection

  JVM's garbage collection mechanism, in case of sufficient memory, unless you explicitly call System.gc (), otherwise it will not be garbage collected; garbage collection will automatically run in a well-memory conditions.

A reference counting algorithm

1. Definitions: reference counting algorithm will add a reference to an object counter, whenever a reference to the place of his time, the counter is incremented; when fail counter reference value is decremented by one. When the counter is 0, the object can be recovered.

2. Disadvantages: the presence of a circular reference, resulting in two circular reference memory object can not be released. There is no a JVM garbage collection is achieved using this algorithm.

3. Status: mainstream Java virtual machine does not use reference counting algorithm to manage memory, because it is difficult to solve the problem of circular references.

Second, the reachability analysis algorithm

1. idea: by a series of "GC Roots" object as a starting point, to start downward from the node search, the search path traversed referred to as "the chain of references." When an object is not connected to the GC Roots any references chain, that is unreachable from the GC Roots to this object, then it proves that this object is not available. As shown in Object5, Object6, Object7 although related to each other, but they are not up to the GC Roots, so they are determined as an object recyclable.

 

 

 Some objects as the root object, JVM think the root object can not be recycled, and the object referenced by the root object is non-recyclable. In the Java language, can be used as an object GC ROOT contains the following:

(1) Virtual Machine stack (Local Variable Table stack frame) referenced formation ah

(2) method of the object in the static attribute references

(3) The method of constant reference object region

(4) Local Native method stacks subject method references.

2. Even if unreachable in reachability analysis method of the object, nor is Feisibuke, to really declared dead objects, twice as long as the experience of the labeling process:

(1) If, after conducting objects reachability analysis, found no references to the chain connected to the GC Roots, it is first referenced.

(2) determining whether the object is necessary to perform finallize (), if the covering finallize object (), or finallize () method has been covered over, the virtual machine is not necessary to perform this as.

  1. If finallize an object () method performs slowly, or even an infinite loop, then causes other objects FQueue queue wait forever, and even cause the entire system to crash recovery, because the object in FQueue of waste can not be recycled.
  2. If an object is determined to be necessary to perform a finalize () method, the object will be placed in a queue F-Queue, and established by a virtual machine at a later time. Finalizer low priority thread to execute. Execution here is a virtual opportunity to trigger this method, but do not promise to wait for the process is finished, the reason for this is: finalize () method is the last opportunity to target escaped death fate, if an object finalize () method successfully rescue myself, and a collection of references to any object on the chain associates, such as his own (this) assigned to a variable or class member variables, it will be removed soon recovered at the time of the second mark.
  3. If the object is not managed to escape, then basically it is really recovered ( second mark ).

Finallize any method of an object will automatically call the system, once the recovery is facing down if the object of his finalize () method will not be executed. Try to avoid finalize method, because it is just for C / C ++ programmers to more easily accept the java made of a compromise, his run costly, high uncertainty, unable to express the calling sequence of individual objects. --- To be continued

Guess you like

Origin www.cnblogs.com/bigdata-stone/p/12041882.html