Strong references in Java (strong reference), soft references (softreference), weak references (weakreference), virtual references (phantomreference)

Source: Internet
Author: User

Before looking at the first time in an in-depth understanding of the Java Virtual Machine, I contacted the relevant noun, but did not understand that the object obj = new Object () is a strong reference when it is similar to this operation. Strong references are not reclaimed by GC until GC roots is unreachable. And the other three nouns are not clear, because they are not really used. By looking at the source code for soft references, weak references, and virtual references, you can see that all three of these classes are inherited from reference.

A concept
1.1 Soft references (SoftReference)
The soft reference I understand means that even if the reference object is not being used, the GC is not recycled immediately, but only when the heap memory is not enough. Typically used for caching objects because you want to keep them as long as possible in memory. The time that a soft reference points to a reference object that is stuck in the heap memory is controlled by the size of the currently available heap memory. There are two fields in a soft reference: clock and timestamp. The clock records the time of initialization or GC, and the Get Reference object is updated when the timestamp is the same as clock time. If clock and timestamp are spaced more than maxinternal, it is considered that the reference object has not been used for a long time to reclaim the referenced object, or it will continue to exist. The time of maxinternal is determined by the current available heap memory size. Specific JVM source code can refer to: HTTPS://MP.WEIXIN.QQ.COM/S/PPXNEUI57GD-YNNFQWGXBG
1.2 Weak references (WeakReference)
A weak reference means that if the Reference object GC roots is unreachable, the GC is recycled. The get operation may return a reference object (if there is no GC), otherwise the return is empty.
1.3 Virtual references (phantomreference)
A virtual reference cannot return a reference object because the get operation always returns null and the feeling is a vague reference, so this is the reason for calling it a virtual reference.
Two examples

Constructs an object testobj, which is 1M in size.
JVM parameter settings:
-xms8m Initial Heap Size
Maximum value of the-xmx8m heap
-xmn4m the size of the young generation
-xx:+printgcdetails
The Billy of-xx:survivorratio=8 Eden:survivor is 8:1

First case:

GC Process:

Analysis:
eden:3072k, from:512k, to:512k, paroldgen:4096k
When calling System.GC, the object's strong reference also exists, so three reference objects will enter the old age.
Second case:

GC Process:

Analysis:
Since the young generation can no longer allocate more than 1M of space, and the old age will not fit, so the full GC will occur, weak references and virtual references to the object will be recycled, and soft references exist. Cost[0],cost[1] into the old age, and at this time the younger generation should have only one cost[2] object, why will occupy eden:3072k space 69%, which makes people very puzzled???
The third case:

GC Process:

Analysis:
Since the eden:3072k space has been used 69%, it is not possible to put down a 1M object, only the full GC (when preparing to trigger a young GC, if the statistics are found that the average promotion size of young GC before the current old Gen is larger than the remaining space, Does not trigger the young GC but instead triggers the full GC. Full GC and Young GC Relationship reference: https://www.zhihu.com/question/41922036/answer/93079526), so the object referred to by the soft reference is eventually retracted. Sure enough, the soft reference survives long enough and only heap memory is sufficient.

Strong references in Java (strong reference), soft references (softreference), weak references (weakreference), virtual references (phantomreference)

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.