In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the four common references in Java". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "what are the four common references in Java" together!
Since JDK 1.2, object references have been classified into four levels, enabling programs to control object life cycles more flexibly. These four levels are, from high to low, strong, soft, weak, and virtual.
1. strong reference
The citations described earlier in this chapter are actually strong citations, which are the most commonly used. If an object has a strong reference, it is similar to an essential household item, and the garbage collector will never recycle it. When out of memory space, the Java virtual machine would rather throw OutOfMemoryError errors, causing the program to abort, than solve the out-of-memory problem by arbitrarily reclaiming objects with strong references.
2. Soft Reference
If an object has only soft references, it is similar to a tangible commodity. If there is enough memory space, the garbage collector will not reclaim it, and if there is not enough memory space, it will reclaim the memory of these objects. As long as the garbage collector doesn't collect it, the object can be used by programs. Soft references can be used to implement memory-sensitive caching.
Soft references can be used in conjunction with a Reference Queue, and if the object referenced by the soft reference is garbage collected, the Java Virtual Machine adds the soft reference to the reference queue associated with it.
3. Weak Reference
If an object has only weak references, it is similar to a tangible commodity. Weak references differ from soft references in that objects with only weak references have shorter lifetimes. As the garbage collector thread scans the memory region it administers, once it finds an object with only weak references, its memory is reclaimed regardless of whether the current memory space is sufficient. However, because the garbage collector is a low-priority thread, objects with only weak references may not be found quickly.
Weak references can be used in conjunction with a Reference Queue, and if the object referenced by the weak reference is garbage collected, the Java Virtual Machine adds the weak reference to the reference queue associated with it.
4. Phantom Reference
As the name implies, a "virtual reference" is a virtual reference. Unlike other references, a virtual reference does not determine the life cycle of an object. If an object holds only virtual references, it can be garbage collected at any time, just as if it had no references.
Virtual references are primarily used to track the garbage collected activity of objects. A virtual reference differs from soft and weak references in that it must be used in conjunction with a Reference Queue. When the garbage collector is ready to reclaim an object, if it finds that it has a virtual reference, it adds the virtual reference to the reference queue associated with it before reclaiming the object's memory. A program can determine whether a referenced object is about to be garbage collected by determining whether a virtual reference has been added to the reference queue. If the program finds that a virtual reference has been added to the reference queue, it can take the necessary action before the referenced object's memory is reclaimed.
In this book,"quote" can be used as either a verb or a noun, and readers should distinguish the meaning of "quote" according to the context.
Three classes are provided in the java.lang.ref package: the SoftReference class, the WeakReference class, and the PhantomReference class, which represent soft, weak, and virtual references, respectively. The ReferenceQueue class represents a reference queue, which can be used in conjunction with these three reference classes to track the Java Virtual Machine's activity of reclaiming referenced objects. The following program creates a String object, ReferenceQueue object, and WeakReference object:
//Create a strong reference String str = new String("hello"); //Create a reference queue, which is a generic tag, indicating that the String object reference is stored in the queue ReferenceQueue rq = new ReferenceQueue(); //Create a weak reference, which refers to the "hello" object and is associated with the rq reference queue//Create a generic tag, indicating that WeakReference will weakly reference the String object WeakReference wf = new WeakReference(str, rq);
After the above program code is executed, the relationship between references and objects in memory is shown in Figure 11-10.
Figure 11-10. The "hello" object has both strong and weak references.
In Figure 11-10, solid arrows indicate strong references and dashed arrows indicate weak references. As you can see, the hello object is strongly referenced by str and weakly referenced by a WeakReference object, so the hello object is not garbage collected.
In the following program code, set the str variable that references the "hello" object to null, and then get a reference to the "hello" object through the get() method of the WeakReference weak reference:
String str = new String("hello"); //① ReferenceQueue rq = new ReferenceQueue(); //② WeakReference wf = new WeakReference(str, rq); //③ str=null; //④ Cancel strong references to "hello" objects String str1=wf.get(); //④ Str1 references "hello" objects if "hello" objects are not recycled//rq.poll() returns null Reference if "hello" objects are not recycled
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.