Strong (strong), soft (soft), weak (weak), virtual (phantom) references

Source: Internet
Author: User

Https://github.com/Androooid/treasure/blob/master/source/lightsky/posts/mat_usage.md

1.1 GC Root

Java Virtual machine through accessibility (reachability) to determine whether an object is alive, the basic idea: the "GC Roots" object as the starting point to search down, the path of the search is called the chain of reference, when an object to the GC Roots no reference chain connected (that is unreachable), The object is judged to be an object that can be recycled, and vice versa.

GC roots can be any of the following objects

    • An object on the call stack (the calling stack) on the current thread (for example, method parameters and local variables)
    • Classes loaded by the thread itself or system class loader (ClassLoader)
    • Native Active objects reserved by code (local code)
1.2 Memory leaks

The object is useless but still up (not released) and the garbage collector cannot be reclaimed.

1.3 Strong (strong), soft (soft), weak (weak), virtual (phantom) Reference strong references

Ordinary Java references, our usual new object is: StringBuffer buffer = new StringBuffer(); If an object is reached by a chain of strong references, it is not garbage collected. You certainly don't want the references you're using to be recycled by the garbage collector. However, for objects in the collection, they should be removed when not in use, otherwise it will consume more memory and cause a memory leak.

Soft Reference

When the object is soft reference, the GC will request more memory from the system, rather than reclaim it directly, and reclaim it when the memory is low. So the soft reference is suitable for building some cache systems, compared to the chip cache.

WeakReference

WeakReference does not force the object to be saved in memory. It has a relatively short life cycle, allowing you to use the garbage collector's ability to weigh the accessibility of an object. As the garbage collector scans the area of memory it governs, once the GC discovers that the object is WeakReference, it is placed in the Referencequeue, and then reclaimed by the next GC. The WeakReference<Widget> weakWidget = new WeakReference<Widget>(widget); system provides us with Weakhashmap, similar to HashMap, except that its key uses weak reference. If a key of Weakhashmap is reclaimed by the garbage collector, the entity is also automatically removed.

Since WeakReference is more likely to be recycled by GC, you need to use Weakobj.get () to determine whether the destination object reference has been reclaimed before using it.

Reference Queque

Once Weakreference.get () returns null, the object it points to is garbage collected, so the WeakReference object is useless, which means you should do some cleanup. For example, in the Weakhashmap to remove the recovered key from the map, to avoid the useless weakreference growing. Referencequeue can make it easy for you to track dead references. The constructor of the WeakReference class has a referencequeue parameter that, when the object being pointed to is garbage collected, places the WeakReference object in Referencequeue. This way, traversing referencequeue can get all the recycled weakreference.

Phantom Reference

and Soft,weak reference is a big difference, its get () method always returns NULL. This means that you can only use the phantomreference itself, but not the object it points to. When the object that WeakReference points to becomes weak (weakly reachable), it is immediately placed in Referencequeue, which occurs before finalization, garbage collection. Theoretically, you can "revive" the object in the Finalize () method (to point a strong reference to it, and the GC does not recycle it). But it can't be resurrected. The object that phantomreference points to. And Phantomreference was placed in the Referencequeue after garbage collection, unable to revive.

For more discussion on Phantom reference, please refer to: understanding-weak-references

Strong (strong), soft (soft), weak (weak), virtual (phantom) references

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.