In-depth understanding of Java Virtual Machines Learning note four/garbage collector GC Learning/One

Source: Internet
Author: User
Tags throwable

Grabage Collection GC three things the GC needs to accomplish:What memory needs to be recycled? When do I recycle? How to recycle?
in the various parts of the Memory runtime area: program counters, virtual machine stacks, local method stacksThese 3 regions are born with threads and are extinguished with threads. The stack frame in the stack methodically executes the stack and stack operation as the method enters and exits. The amount of memory allocated in each stack frame is basically known when the class structure is determined, so the memory allocations and recoveries in these areas are deterministic, and there is no need to think too much about recycling in these areas. Because the method ends or the thread ends, the memory is naturally recycled. and Java heap and method areaIt's not the same, the memory of multiple implementation classes in one socket may be different, and multiple branches in one method may require different memory, and we'll only know which objects are created when the program is running, and this part of the memory allocation and recycling is dynamic. Our garbage collector is concerned with this part of the memory.
Heap memory for this large number of object instances, the garbage collector first to determine which of these objects are "alive" and which are "dead"
Reference counter AlgorithmAdds a reference counter to the object, which is incremented by 1 whenever it is referenced, minus one when the reference is lost. At any moment, the calculator value of 0 means that the object can no longer be used. Problem: It is difficult to solve the problem of circular reference between objects
Accessibility Analysis AlgorithmBasic idea: Through a series of objects called "GC Roots" as 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 Java, objects that can be used as GC roots include the following: 1. Object 2 referenced in the virtual machine stack (local variable table in the stack frame), object 3 referenced by the class static property in the method area, object 4 in the method area of the constant reference, and the object referenced in the local method stack (that is, generally speaking, native method)
No matter which algorithm is related to the reference, but the reference how to understand it? What kind of reference is useful, and what kind of reference is useless?The references are differentiated after JDK1.2, and the four reference intensities are progressively decremented: Strong ReferencesRefers to the general existence of the program, such as: Object obj = new Object (), such a reference, as long as the strong reference is still in the garbage collector will never reclaim the referenced object. Soft ReferencesUsed to describe some objects that are useful but not necessary. For objects associated with soft references, the system will collect two of these objects in the recycle range before a memory overflow exception occurs. If there is not enough memory after this collection to throw a memory overflow exception Weak referencesWeak references are also used to describe some non-essential objects whose strength is weaker than soft references, and objects associated with weak references can only survive until the next garbage collection occurs. Whether the current memory is sufficient or not, it is reclaimed by the garbage collector. Virtual ReferenceAlso known as Phantom references or phantom references. The existence of an object with a virtual reference does not affect its lifetime at all, nor can it obtain an instance of an object through a virtual reference. The only purpose of setting a virtual reference for an object is to be able to receive a system notification when the object is reclaimed by the collector.
To survive or to die?In the accessibility analysis algorithm, even objects that are unreachable are not necessarily dead. To really determine the death of an object, go through the process of marking at least two times: first mark:      Object After accessibility analysis, there is no reference chain connected to GC roots       and a filter was made, The criteria for filtering is that the object has no need to execute the Finalize () method           When the object does not override the Finalize () method, or the Finalize () method has been executed by the virtual machine. The task does not need to execute the Finalize () method           When the object needs to execute the Finalize () method, it is placed in a queue called F-queue, which is automatically executed by the virtual machine for the second time:      finalize () method is the last chance for an object to escape death, the GC marks the F-queue queue for a second time,      If the object succeeds in saving itself in the Finalize () method:           All you need to do is establish a relationship with any object on the reference chain.       Then the second token, it will be removed from the collection that is about to be recycled. If the object has not escaped at this stage, it is recycled. Here is the code, the contents of the original book:
Package com.smile.three;/** * This code demonstrates two points: * 1. Objects can be saved by the GC itself.     * 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 * @author in-depth understanding of JVM author Zhou Zhiming Zzm */public class FINALIZEESCAPEGC {//Definition Object     public static FINALIZEESCAPEGC Save_hook = null;     The live object can be called by the method public void IsAlive () {System.out.println ("Yes, I am still alive:)");         }//Rewrite Finalize () method @Override protected void Finalize () throws Throwable {super.finalize ();         System.out.println ("Finalize Mehtod Executed!finalize method was executed");     Finalizeescapegc.save_hook = this; public static void Main (string[] args) throws Throwable {//Create object and assign value Save_hook = new FINALIZEESCAPEGC         ();         The object successfully saved itself for the first time Save_hook = null;         System.GC ();         Because the finalizer method has a low priority, pause for 0.5 seconds to wait for it to Thread.Sleep (500);         if (Save_hook! = null) {save_hook.isalive ();         } else {System.out.println ("No, I am dead:("); }//The following code with the aboveis exactly the same, but this time the rescue failed save_hook = null;         System.GC ();         Because the finalizer method has a low priority, pause for 0.5 seconds to wait for it to Thread.Sleep (500);         if (Save_hook! = null) {save_hook.isalive ();         } else {System.out.println ("No, I am dead:("); }     }}

Note One thing: the Finalize () method of any object will only be executed once by the system. So at the time of the second recovery, its finalize () method fails.
Recycling Method AreaThe method area, known as the permanent generation in the generational algorithm, is much lower than the new generation of "cost-effective" recovery in the permanent generation of GC.     Content for permanent garbage collection: Obsolete constants: Constants that enter a constant pool without any references, or even constants in a constant pool may be recycled.          Useless class: The Useless class Standard: 1, all instances of the class are recycled, and no instances of that class exist in the Java heap. 2. The classloader that loaded the class has been reclaimed 3, the corresponding Java.lang.Class object of the class is not referenced anywhere, and the method of accessing the class can be accessed anywhere by reflection.

In-depth understanding of Java Virtual Machines Learning note four/garbage collector GC Learning/One

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.