"In-depth understanding of Java Virtual Machines"--garbage collector and memory allocation policy

Source: Internet
Author: User

The GC needs to be completed:
    • Which memory needs to be recycled
    • When is it recycled?
    • How to Recycle
How to determine if an object is no longer used
  • Reference counting algorithm
    Add a reference counter to the object, and when there is a place to reference it, the counter value is added 1, and when the reference fails, the counter value is reduced by 1, and when the counter value is 0, the object cannot be used again. However, it does not solve the problem of circular references.

    public class ReferenceCountingGC {      public Object instance = null;    public static void testGC(){        ReferenceCountingGC objA = new ReferenceCountingGC ();        ReferenceCountingGC objB = new ReferenceCountingGC ();        // 对象之间相互循环引用,对象objA和objB之间的引用计数永远不可能为 0        objB.instance = objA;        objA.instance = objB;        objA = null;        objB = null;        System.gc();}}
    The last two sentences of the code above Obja and OBJB are assigned null, that is, Obja and OBJB point to the object is no longer accessible, but because they reference each other, resulting in their reference counters are not 0, the garbage collector will never reclaim them.
  • Accessibility analysis algorithm
    By using a series of objects called "GC Roots" as starting points, the search is traversed by a path called a reference chain (Reference Chain), proving that the object is not available when an object is connected to a GC Roots without any reference chain. For example, objects Object5, OBJECT6, and Object7 are associated with each other, but they are not accessible to GC roots, so they are judged as recyclable objects.

    In the Java language, objects that can be used as GC roots include:
    • Objects referenced in the virtual machine stack (local variable table in the stack frame)
    • method to go to the object referenced in the class static property
    • Objects referenced by constants in the method area
    • The object referenced by JNI (that is, generally speaking, the native method) in the local method stack.
About references

Whether through the reference counting algorithm or the accessibility analysis algorithm, it is related to "reference" to determine whether an object survives.
Declare an object to be truly invalid, at least two times the marking process, if the object after the accessibility analysis found that there is no reference chain connected to the GC roots, it will be the first time to be marked and filtered, the condition is whether this object is necessary 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".
About references in Java:

    • Strong references: Common in program code, such as "Object obj = new Object ()" reference, as long as a strong reference exists, the garbage collector will never reclaim the referenced object.
    • Soft references: Used to describe some objects that are useful but not necessary. For objects that are associated with soft references, these objects are then listed in the collection scope for a second collection before the system will have a memory overflow exception. If this collection does not have enough memory, a memory overflow exception will be thrown. After JDK 1.2, the SoftReference class was provided to implement soft references.
    • Weak references: Used to describe non-required 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 objects that are linked by weak references are reclaimed regardless of whether the current memory is sufficient. After JDK 1.2, the WeakReference class was provided to implement weak references.
    • Virtual reference: Also known as Phantom Reference or phantom reference, it is the weakest reference relationship. Whether an object has a virtual reference exists, does not affect its lifetime at all, and cannot obtain an object instance through a virtual reference. The only purpose of setting a virtual reference association for an object is to be able to receive a system notification when the object is reclaimed by the collector. After JDK 1.2, the Phantomreference class is provided to implement the virtual reference.

The garbage collection of the permanent generation mainly recycles two parts: obsolete constants and useless classes. Compared to the obsolete constants, it is relatively harsh to determine whether a class is a "useless class", and the class needs to meet the following three conditions: (do not satisfy certain not to recycle, satisfy not necessarily recycle, distinguish from invalid object to recycle)
? All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap
? The ClassLoader that loaded the class have been recycled
? The corresponding Java.lang.Class object of this class is not referenced anywhere, and cannot be accessed from anywhere by reflection on the method of the class

Garbage collection algorithm
    • Tag-Purge (Mark-sweep) algorithm
      All objects that need to be reclaimed are marked first, and all tagged objects are collected uniformly after the tag is completed. The tagging process is marked by using reference counting or accessibility analysis.
      Insufficient:
      • Efficiency issues: Marking and clearing two processes is inefficient
      • Space problem: After the tag is cleared, there is a lot of discontinuous memory fragmentation, too much space fragmentation, which leads to the inability to find enough contiguous memory and the need to start another garbage collection action in the future when a larger object needs to be allocated during the program's run.
    • Replication Algorithms
      Divide available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is used up, copy the surviving object to the other piece, and then clean up the used memory space once.

    • Tagging-sorting algorithms
      The tagging process is still the same as the tag-purge algorithm, but the next steps no longer directly clean up the recyclable objects, 20 allows all surviving objects to move toward one end, and then cleans up memory outside the end boundary directly.

    • Generational collection Algorithms
      According to the different life cycle of the object to divide the memory into several blocks, the Java heap is generally divided into the new generation and the old age, according to the characteristics of each age to adopt the most appropriate collection algorithm. In the Cenozoic, when every garbage collection found that a large number of objects died, only a small number of survival, then choose the replication algorithm, and the old age because the object survival rate is high, there is no extra space for the allocation of security, you must use the "mark-clean" or "tag-collation" algorithm to be recycled.

"In-depth understanding of Java Virtual Machines"--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.