Deep understanding of the JVM: garbage collector and memory allocation policy

Source: Internet
Author: User

The heap contains almost all of the object instances of the Java world, and the first thing the garbage collector can do before it recycles the heap is to determine which of these objects are still alive and which are dead. There are several ways to determine whether an object's life cycle is over
Reference Counting Method
The action is to add a reference counter to the object, whenever there is a place reference, the value of the counter is incremented by 1, and when the reference fails, the counter is reduced by 1, and any object with counter 0 at any time is impossible to be used again. Objectively speaking, the reference counter algorithm is simple, the decision efficiency is very high, in most cases he is a good algorithm. But the reference counter is defective
As a simple example, both object A and object B have field instance, the assignment command obja.instance = Objb,objb.instance=obja, and in addition, these two objects have no reference, in fact, these two objects are no longer accessible, But they refer to each other, causing the reference data device to be 0, unable to notify the CG collector to collect them.

 Public classreferencecountinggc{ PublicObject instance =NULL;Private StaticFinalint_1MB =1024x768*1024x768;//The only meaning of this member property is to occupy a bit of memory so that it can be seen in the GC log whether it is recycled    Private byte[] Bigsize =New byte[2*_1MB]; Public Static voidtestgc{REFERENCECOUNTINGGC Obja =NewREFERENCECOUNTINGGC (); REFERENCECOUNTINGGC OBJB =NewREFERENCECOUNTINGGC ();        Obja.instance = OBJB;        Objb.instance = Obja; Obj. A =NULL; Obj. B =NULL; }}

With GC logs, the virtual machine does not recycle them because the two objects refer to each other, which indicates from the side that the virtual machine is not referencing counters to determine if the object is alive.
Accessibility Analysis Algorithm
The basic idea of this algorithm is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, searching through the path called the 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
1. Variables referenced in the virtual machine stack (local variable table in the stack frame)
2. Objects referenced by class static properties in the method area
3. Objects referenced by constants in the method area
4. Objects referenced by JNI (native method) in the local method stack
Citation Analysis
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. This definition is too narrow, an object in this definition is only referenced and not referenced in two states, for how to describe some "tasteless, discard" object is powerless. We want to describe a class of objects that can be stored in memory when there is enough memory space, and if the memory space is still very tense after garbage collection, discard the objects.
After JDK1.2, Java extended the reference to include strong references, soft references, weak references, four of virtual references, and decreasing intensity in turn.
1. Strong references: Common in programs, such as Object obj = new object (); CG will never be recycled as long as a strong reference is present
2. Soft references: Describes some useful, but not necessary, objects. For soft-referenced objects, these objects are listed in the Reclaim scope for two recoveries before the system is about to have a memory overflow, and if not enough memory is recovered to report a memory overflow, JDK1.2 provides a softreference class to implement the soft reference.
3, weak references: Also describes non-required objects, the weakly referenced objects can only survive until the next garbage collection. When the garbage collector is working, the WeakReference class is used to implement a weak reference, regardless of whether the memory is sufficient or not.
4, Virtual reference: Phantom Reference or Phantom Reference, the weakest. Whether an object has a null reference exists, does not affect its time-to-live, nor can it obtain an object instance by means of a null reference.
To survive or to die?
Even in the accessibility analysis algorithm can not reach the object, is not "dead", at this time they temporarily out "probation" in order to truly declare an object to die, at least two times to go through the marking process: if the object after the accessibility analysis found that there is no reference chain connected to the GC roots, Then he will be marked for the first time and screened with the condition that this object is necessary to perform the Finalize method. When the object does not overwrite the Finalize method, or if the Finalize method has been called by the virtual machine, the virtual machine treats both cases as unnecessary execution.
Class, you can tell the garbage collector what to do, and the method inherits from the object class. The garbage collector calls the Finalize method of the object before it is permanently deleted from the heap. Note that it is not possible to ensure exactly when the garbage collector calls the method, nor does it guarantee the order of methods that invoke different objects. The Finalize method of both objects may be called in any order, even if one object contains a reference to another object, or if another object was freed long before the object was disposed. If a particular order must be guaranteed, you must provide your own unique cleanup method.
Recycling Method Area
The method area is the permanent generation in the hotspot virtual machine, and the garbage collection of the permanent generation mainly recycles two parts: obsolete constants and useless classes. Take the constant string "abc" as an example, has entered the constant pool, but the current system does not have any string object called ABC, in other words, no string object refers to the constant pool of ABC, if memory recycling occurs at this time, this ABC constant will be cleaned out by the system of the constant pool. The symbolic references to other classes, interfaces, methods, and fields in a constant pool are similar.
It is relatively simple to determine whether a constant is an obsolete constant, and it is much more demanding to determine whether a class is a useless class, and a useless class to satisfy the following conditions:
1. All instances of this class have been reclaimed and no instances of the class exist in the Java heap.
2. The classloader that loaded the class have been recycled
3. The Java.lang.class object of this class is not referenced anywhere and cannot be accessed from anywhere through reflection.

Deep understanding of the JVM: garbage collector and memory allocation policy

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.