Analyzing the target JVM death [two]

It summarizes the various parts of the Java runtime memory area on the article, which the program counter, the virtual machine stack, native method stacks the same three regions and thread life cycle, with the method of execution stack frame stack of the virtual machine to perform respective stack and pop operations, how much memory in each stack frame allocated when substantially finalized class structure has been known. Therefore, memory allocation and GC these areas are equipped with certainty, with the end of the method or threads, memory is naturally recovered. The heap and method area is not the same, the interface implementation class needs more than a memory may not be the same, a method of multiple branches need memory may not be the same, in order to determine the memory allocation is only part of this program is running, garbage collection It is concerned that this memory area. Because the objects are stored in the heap, to determine which objects can be recovered during GC.
Here Insert Picture Description

First, a clear point of view: an object that is not a non-life death. When enough space, some objects may be retained; when the lack of space, some of the objects will be treated as garbage collection.

Analyzing the state of the object 1. The algorithm death

1.1 reference counter algorithm

Dispensing an object in the object header memory storing the number of times an object is referenced, whenever the object being referenced +1 contrast, whenever a reference fails to -1, 0 when it is considered recoverable objects .

Advantages : simple, high performance.
Disadvantages : It is importantNot solve the problem of circular references
Here Insert Picture Description

1.2 reachability analysis algorithm

java (including mainstream into language) is the use of reachability analysis algorithm to determine the state of the object of life and death.

The algorithm is a series of "GC Roots" object as a starting point, a search down from these objects, path through the "chain of references", if an object is not connected to any reference GC Roots chain, then the object is to be recovered .
Here Insert Picture Description
GC Roots can be used as target following four:

  1. VM stack (local variables table stack frame) referenced objects
  2. Native method stacks of objects referenced by Native Method
  3. The method of constant reference object region
  4. Method static property class object referenced by region

2. Object of life and death and referenced relations

Whether counter reachability analysis algorithm or algorithms related references and object references, so the reference object determines the subject of life and death.
java object reference into four categories: strong reference 1, reference 2 soft, weak references 3, 4. The virtual references cited descending order of intensity.

1. A strong reference: Object obj = new Object () ; the most common in the code, as long as the reference is still strong, the garbage collector will never recover the object referenced, it is recommended to use complete object manually set to null, obj = null prevent memory leaks.

2. Soft references: jvm before the OOM, will try to recover the soft reference object pointing.

3. Weak references: JVM software recovered by pointing at the object GC occurs.

4. imaginary reference: Unable to get through the virtual reference object instance, set a unique role of the virtual object references: receive a notification when the object reference system to be garbage collected.

3. Marked for Death and Salvation

After a certain object reachability analysis, the true mark of death to go through the process twice.

If the object does not reach the reference GC Roots chain is connected, it will be the first mark, and then screening with two conditions:
1. the object of rewriting the Object Finalize ();
Finalize 2. subject () is not performed (finalize the object () only once).

Does not meet the mark for the second filter criteria is dead, meet the filter criteria will be added to a queue that object later a low priority thread created by a virtual machine execution.

If the finalize () re-established and associated with any object on a chain of references, this object is not dead.

public class FinalizeTest {
    private static FinalizeTest finalizeTest = null;

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println("finalize方法执行了");
        FinalizeTest.finalizeTest = this;
    }

    public static void main(String[] args) throws InterruptedException {
        finalizeTest = new FinalizeTest();

        //对象第一次成功拯救自己
        finalizeTest = null;
        System.gc();
        //因为finalize方法优先级很低,所以暂停1秒等待一下
        Thread.sleep(1000);
        if(finalizeTest!=null){
            System.out.println("对象被拯救了");
        }else{
            System.out.println("对象死亡了");
        }

        //这段与上面完全相同,但是却拯救失败了
        finalizeTest = null;
        System.gc();
        Thread.sleep(1000);
        if(finalizeTest!=null){
            System.out.println("对象被拯救了");
        }else{
            System.out.println("对象死亡了");
        }
    }
}

Execution results are as follows:
Here Insert Picture Description

Not recommended finalize () rescue of the following reasons:
1. only once
2. Run costly
3. The call can not guarantee the order of each object

Published 11 original articles · won praise 0 · Views 615

Guess you like

Origin blog.csdn.net/fei1234456/article/details/104733165