In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the references to java objects?". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The reference reference of java object is divided into several forms: strong reference, soft reference, weak reference and virtual reference.
1. Soft Reference (soft reference)
The objects referred to will not be cleared even if there is no Direct Reference, and will not be cleared until the jvm is out of memory and there is no Direct Reference.
Application scenarios: commonly used to design caches, not only can cache objects, but also will not easily cause insufficient memory.
2. Weak Reference (weak reference)
As soon as GC discovers a weak reference relationship, it begins to collect objects.
Application scenario: want to view some object information without affecting the collection of objects by GC.
Interaction with GC:
1. Set referent as a null reference
two。 Set the target object in the heap to "finalizable"
3. Finalize () is executed first to free up memory, and the soft reference is added to the RefernceQueue.
ReferenceQueue can be null in soft renference and weak reference, but it must be in virtual applications!
3. PhantomReference (virtual reference)
Interaction with GC:
1. Do not set referent as a null reference; instead, set the target object in the heap to "finalizable"
two。 The soft reference is added to the RefernceQueue first. Then collect objects and free memory
Note: ReferenceQueue can be null in soft renference and weak reference, but it must be in a virtual reference!
4. Abstract Reference analysis
There are four classes in the java.lang.ref package, and their relationship is that PhantomReference,WeakReference,SoftReference inherits the abstract class Reference. In the Reference class, there is a thread subclass (ReferenceHandler), which is opened in the static area to monitor the pending field in real time.
The pending is assigned by jvm, and when the reachable state of the referent object within the Reference changes, jvm puts the Reference object into the pending linked list.
Once relevent= null, garbage collection marks WeakReference's referent as unreachable get () returns null, and triggers the ReferenceHandler thread to process pending by assigning a value to pending.
What the ReferenceHandler thread needs to do is to enqueue the pending object, but by default the queue we provide, that is, what is passed in from the constructor is null. In fact, we use the ReferenceQueue.NULL,Handler thread to judge that queue is ReferenceQueue.NULL and do not operate. Only non-ReferenceQueue.NULL queue will enqueue the Reference.
This is the end of the content of "what are the references to java objects"? thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.