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 to complete in GC: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 thread-born. is extinguished with the thread. The stack frame in the stack runs the stack and stack operations methodically 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 when the thread ends. The memory is naturally recycled. The Java heap and the method area are different. The memory required for multiple implementation classes in one socket may be different. The memory required for multiple branches in a method may not be the same, we 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 infers which of these objects are "alive" and which are "dead"
The reference counter algorithm adds a reference counter to the object and adds 1 to the counter whenever it is referenced. Calculator minus one when you lose a quote.

At any given moment, a calculator value of 0 means that the object is no longer available. Problem: Very difficult to solve the problem of circular reference between objects
The basic idea of the accessibility analysis algorithm is to use a series of objects called "GC Roots" as the starting point. Starting from these nodes, the search path is called the reference chain (Reference Chain), proving that this object is not available when an object is not connected to a GC roots no matter what the reference chain is.

In Java. The 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 practical, and what kind of reference is useless?The references are differentiated after JDK1.2. These four reference intensities gradually decrease in turn: Strong References??? refers to the general existence of a program, such as: Object obj = new Object (), such a reference. Just a strong quote is still there. The garbage collector never reclaims the referenced object. Soft References??? is used to describe some practical but not necessary objects. The object associated with the soft reference. The system will collect two of these objects in the recycle range before the memory overflow exception occurs. Assuming that there is not enough memory after this recovery to throw a memory overflow exception Weak referencesThe weak reference is also used to describe some non-essential objects. Its strength is weaker than soft reference, and the object associated with the weak reference. 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 reference ??? is also called Phantom Reference or Phantom Reference.

Whether an object has a virtual reference exists. It has no effect on its lifetime, nor does it get 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.
survival or death in an accessibility analysis algorithm. Even if the object is unreachable. It's not necessarily death. To really determine the death of an object, you have to go through the process of marking at least two times: the first time tag:??? object after accessibility analysis, there is no reference chain connected to GC roots??? and a filter was made. The criteria for filtering is whether the object is necessary to run the Finalize () method????? When the object does not override the Finalize () method, or the Finalize () method has already been run by the virtual machine, it does not feel necessary to run the Finalize () method????? When the object needs to run the Finalize () method, it is placed in a queue called F-queue, which is run by the virtual machine on its own initiative. The finalize () method is the last chance for an object to escape death, and the GC marks the F-queue queue for a second time. Suppose the object succeeds in saving itself in the Finalize () method:????? It is only possible to establish a relationship with whatever object is on the reference chain.

??? Then the second token, it will be removed from the collection that is about to be recycled. Assuming that 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. Such a chance of self-salvation is only once. Because the Finalize () method of an object is only invoked once at most by the system itself * @author in-depth understanding of JVM author Zhou Zhiming Zzm */public class FINALIZEESCAPEGC {//define Object public stat     IC FINALIZEESCAPEGC save_hook = null;     The live object can call 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 is run");     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 ();         Since the finalizer method has a very 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:("); }//below thisThe section code is exactly the same as above, but this time it failed Save_hook = null;         System.GC ();         Since the finalizer method has a very 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: No matter what the Finalize () method of an object will only be run once by the system.

So at the time of the second recycling. Its finalize () method is invalidated.
The recovery method area method area, in the generational algorithm is called the permanent generation, but in the permanent generation GC is compared to the new generation of recovery "cost-effective" is much lower. The content of the permanent garbage collection:???? No matter what the reference is, constants that enter the constant pool, or even constants in a constant pool, may be recycled.

??? Useless class:????? Useless class standard:????? 1. All instances of this class are recycled and no instances of the class exist in the Java heap.????? 2. ClassLoader loaded into this class has been recycled????? 3. The corresponding Java.lang.Class object of this class is not referenced anywhere and cannot be used to access the method of the class in any place by reflection.

GC recycling involves a lot of content, and I'm still learning that this series of blogs will continue to be updated.


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

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.