Record Java garbage collection mechanism and several references

Source: Internet
Author: User

I. Java garbage collection mechanism

Java's garbage collection mechanism (Java garbage collection) is the ability of a Java virtual machine to dynamically reclaim the heap memory space occupied by objects without any references in an idle time, in an unscheduled manner .

Note Where in bold, Java's garbage collection thread is a lower priority thread and when garbage collection is difficult to determine. When some objects are marked as garbage, the garbage collection threads recycle them (rather than reclaim the heap memory space that these objects occupy) when they are run.

Two. What kind of objects will be labeled as garbage objects?

    1. In general, all references to objects are invalidated, and it is not possible for a program to call this object, so the object becomes garbage and should be recycled.
    2. According to this idea, it is easy to think of a reference counting method to determine whether an object is garbage. That is, whenever more than one reference to the object, the reference count plus one, each time one less reference points to the object, the reference count minus one, the reference count is reduced to zero, the object can be recycled.
    3. However, a fatal problem with reference counting is the problem of circular referencing . For example, a circular list, they loop the reference, the reference count will never be zero, but in fact the program has no access to them, they should be recycled. (If you don't understand the circular reference, the fifth step is followed by a code description)
    4. So Java is actually using GC Roots-based accessibility analysis , what is GC Roots? The static variables of all classes, each thread calls the local variables on the stack (in fact we are programmed to access the data from these places), all of these objects, and the objects that are pointed to by these objects, are live objects. The object that the living object points to is also a live object. The GC makes the accessibility analysis through the graph, and the unreachable object is considered a garbage object.
    5. So as long as the GC at the moment, let the program pause to run, and then from the GC roots start analysis, and finally not marked as a live object is garbage.

* The code example explains what a circular reference is:

As shown, suppose we have two classes in which A and B,a classes have a field that is Class B type, a field in Class B is a class A type, and now a Class A object is new and new Class B object, when reference a to the new Class A object, reference B to just new out of Class B object, The fields in the two classes are then referenced to each other so that even if the following is a = null and b = NULL, the Class A object is still referenced by the fields in Class B objects, although both Class A and Class B are now inaccessible, but the reference count is not 0.

Three. What kinds of references are in Java

    • Strong Reference (Strongreference): Strong references are the references we use most commonly, and are the most common references. If an object has a strong reference, the garbage collector will never recycle it (in combination with a strong reference to an object, the object must be up to date, which means the object is alive). When there is not enough memory space, the Java virtual Machine prefers to throw a outofmemoryerror error, which causes the program to terminate abnormally, and does not rely on random recycling of strongly referenced objects to resolve out-of-memory issues. (Précis-writers: never recycle)
    • Soft Reference (SoftReference): If an object has only soft references, enough memory space is available, the garbage collector does not recycle it, and if the memory space is insufficient, the memory of those objects is reclaimed. The object can be used by the program as long as it is not reclaimed by the garbage collector. Therefore, a soft reference can be used to implement a memory-sensitive cache. (Précis-writers: not enough memory to reclaim)
    • Weak reference (WeakReference): The difference between a weak reference and a soft reference is that the weakly referenced object has a shorter life cycle. As the garbage collector thread scans the area of memory it governs, once an object with only a weak reference is found, its memory is reclaimed, regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a low-priority thread, it is not necessarily quick to discover objects that have only weak references. (Précis-writers: recycling is encountered)
    • Virtual Reference (phantomreference): "virtual Reference", as the name implies, is a dummy, unlike several other references, a virtual reference does not determine the object's life cycle. If an object holds only virtual references, it can be reclaimed by the garbage collector at any time, just as there are no references. Virtual references are primarily used to track activities that objects are reclaimed by the garbage collector. One difference between a virtual reference and a soft reference and a weak reference is that the virtual reference must be used in conjunction with the reference queue (Referencequeue). When the garbage collector prepares to reclaim an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory. (Précis-writers: fake)

It is worth distinguishing that soft and weak references are optional when used in conjunction with a reference queue and, if used in conjunction with a reference queue, when the referenced object is garbage collected, the Java virtual machine adds the weak (or soft) reference to the reference queue associated with it. A virtual reference, however, must be used in conjunction with a reference queue.

Record Java garbage collection mechanism and several references

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.