Deep understanding of the Java Virtual Machine family--garbage collector and memory allocation policy (II)

Source: Internet
Author: User
Tags throwable

An algorithm that determines whether an object survives:

Simple version : Add a reference counter to the object, and whenever there is a place to reference it, the counter value is incremented by 1, and when the reference fails, the counter value is reduced by 1. An object with a counter of 0 at any time is impossible to be used again.

But the mainstream Java virtual machine does not have a reference counting algorithm to manage memory, the most important reason is that it is difficult to solve the problem of circular reference between objects.

Accessibility Analytics Algorithm (reachability analysis):

In the mainstream implementation of the mainstream programming language (java,c#, etc.), the accessibility analysis is used to determine whether an object survives.

Algorithm idea: Through a series of objects that become "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain (Reference Chain), when an object to the GC Roots no reference chain connected, it proves that this object is not available.

In the Java language, objects that can be used as GC roots include the following:

1) The object referenced in the virtual machine stack (local variable table in the stack frame);

2) The object referenced by the class static property in the method area;

3) The object referenced by the constants in the method;

4) The object referenced in the local method by JNI (that is, the general native method).

Reference definition in Java: If the value stored in the data of the reference type represents the starting address of another piece of memory, it is called the memory represents a reference.

After JDK1.2, Java extends the concept of references into strong references (strong Reference), soft references (Soft Reference), weak references (Weak Reference), virtual references (Phantom Reference) , 4 reference intensities weaken at a time.

1) Strong references: common in program code. As long as a strong reference exists, the garbage collector never reclaims the referenced object;

2) Soft reference: Used to describe some useful but not necessary objects. For objects associated with soft references, these objects are then listed in the collection scope for a second collection before the system is about to occur with a memory overflow exception. If this collection does not have enough memory, a memory overflow exception will be thrown.

3) Weak references: Also used to describe non-essential objects, the strength is weaker than soft references, and objects associated with weak references can only survive until the next garbage collection occurs. When the garbage collector is working, the object associated with the weak reference is reclaimed regardless of whether the current memory is sufficient.

4) Virtual reference: Also known as Phantom Reference or phantom reference, whether an object has a virtual reference exists at all does not affect its time-to-live. The sole purpose of setting a virtual reference association for a reference is to be able to receive a system notification when the object is reclaimed by the collector.

To survive or to die?

To actually declare an object dead, at least two times. Marking process: If the object finds no reference chain connected to the GC roots after the accessibility analysis, it will be first marked and filtered for the first time, and the condition is whether this object is required to execute the Finalize () method. When the object does not overwrite the Finalize () method, or the Finalize () method has been called by the virtual machine, the virtual machine treats both cases as "no need to execute."

Program Listing 3-2

Package secondchapter;/* * This code demonstrates two points: * 1. An object can redeem itself while being GC * 2. This chance of self-help is only one time, because the Finalize () method of an object is only automatically called once by the system */public class FINALIZEESCAPEGC {public static finalizeescapegc save_hook=null;public void IsAlive () {System.out.println ("Yes,i am Still alive:) ");} @Overrideprotected void Finalize () throws Throwable{super.finalize (); System.out.println ("Finalize Method executed!");    Finalizeescapegc.save_hook=this;}    public static void Main (string[] args) throws throwable{save_hook=new finalizeescapegc ();    Object for the first time to successfully save oneself save_hook=null;    System.GC ();    Because the Finalize method has a low priority, pausing for 0.5 seconds waits for it to Thread.Sleep (500);    if (save_hook!=null) {save_hook.isalive ();    }else{System.out.println ("No,i am Dead: (");    }//The code below is exactly the same as above, but it failed in this case//the object first succeeded in saving himself save_hook=null;    System.GC ();    Because the Finalize method has a low priority, pausing for 0.5 seconds waits for it to Thread.Sleep (500);    if (save_hook!=null) {save_hook.isalive ();    }else{System.out.println ("No,i am Dead: ("); }        }}

P67

Deep understanding of the Java Virtual Machine family--garbage collector and memory allocation policy (II)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.