Strongreference, WeakReference, SoftReference in Java

Source: Internet
Author: User

Whether it is Python or Java, as long as the VM, there are strongreference and weakreference concepts, on different VM platforms, may also produce some additional __reference concepts, such as the JVM's SoftReference, Phantomreference

Read at three and break:

1.VM management memory will probably have a reference count.

2.GC recycled memory will probably be traversed from some root object, in managed memory if some objects cannot be traversed, then these objects are garbage and should be recycled.

3. There are some additional memory usage scenarios that are not suitable for GC processing, such as various caches, so-called cache is a bunch of data, may be used at some point in time, or may never be used, so they should be valid for a certain period of time, expire and throw away. If a GC is used to process the data into a data collection object (such as LinkedList), the data in LinkedList is never recycled as long as the LinkedList is referenced.

Strongreference and WeakReference

1.StrongReference is an ordinary object reference, like Object Obj=new object (), and this obj is Strongreference, an object that belongs to a regular object in a VM and is very common.

2.SoftReference is a relatively strong reference, the memory management in the JVM is more complex, the reference is divided into different types, in different memory usage situation, with different strategies, softreference this reference general situation is not to reclaim the referenced object, But in low memory (how much memory is used is not enough memory?) This value can be configured? ), the object is reclaimed when an Oom exception is thrown. Use SoftReference to do cache back better, the data will be put in memory as far as possible, know that reached a threshold, only to start cleaning up the softreference data.

A 3.WeakReference object is a object that can get to the referenced object, but the object's life cycle is not controlled by this reference, that is, the reference does not cause an increase in the reference count, nor does it cause the traversal of the GC traversal to arrive at the referenced So the referenced object can be recycled by GC at any time.

Strongreference, WeakReference, SoftReference in Java

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.