Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the referencing methods in Java

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I would like to talk to you about the citation methods in Java, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Strong reference > soft reference > weak reference > virtual reference

1. Strong citation

Strong quotation, where is strong? The strong point is that "even if JVM overflows, it will not allow GC to casually recycle the memory space occupied by strongly referenced objects." The reason is also easy to understand. If GC recycles the above ref, then the object new Object () will be meaningless, and all subsequent code that uses ref will go wrong.

Strong quotation, when will it expire? Only when the value of the strong reference is set to null or the scope expires, will GC bring the strong reference object into the scope of being recycled.

two。 Soft reference

When JVM has enough memory, GC treats soft references and strong references in the same way, that is, it does not actively recycle soft reference objects; but when JVM runs out of memory, GC will actively recycle soft reference objects.

It can also be understood that JVM only collects soft reference objects before throwing an OutOfMemory.

Strong references can be generated directly through the keyword new, while soft references need to rely on java.lang.ref.SoftReference provided by JDK.

In soft references, weak references, and later virtual references, there is a get () method (which actually inherits the parent class Reference), which is used to get the reference object that the reference points to. As follows, the ObjectDemo object pointed to by the soft reference is obtained through the get () method.

Class ObjectDemo {

Public void method () {}

}

Public class ReferenceDemo {

Public static void main (String [] args) throws Exception {

SoftReference softReference = new SoftReference (new ObjectDemo ())

ObjectDemo obj = softReference.get ()

}

}

3. Weak reference

A weak reference in JDK refers to the java.lang.ref.WeakReference class. When GC carries out garbage collection, it will actively collect weakly referenced objects regardless of whether or not JVM has enough memory at that time.

4. Virtual reference

Virtual references are java.lang.ref.PhantomReference classes in JDK, and virtual references are also known as phantom references or ghost references. Virtual reference is the lowest of all reference levels, even "it makes no difference to an object itself whether or not to use a virtual reference," and we cannot get an object instance through a virtual reference. Virtual references are generally used with reference queues (ReferenceQueue). Its value is that when GC collects an object, if it finds that there is still a virtual reference to the object, it will store the virtual reference in the corresponding reference queue before the object is reclaimed, and the corresponding virtual reference object will not be recycled until the virtual reference is dequeued. Therefore, we can estimate the frequency of GC recycling by analyzing the number of virtual references in the reference queue, or do some additional operations before the virtual reference object is reclaimed. After reading the above, do you have any further understanding of the citation methods in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report