In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "summary of citation knowledge points in Java". In the operation of actual cases, many people will encounter such a dilemma, so 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!
Strong references: by default, objects use strong references (the instance of this object has no other object references, so GC will be recycled)
Soft references: soft references are an application provided in Java that is suitable for caching scenarios (GC only if there is not enough memory)
Weak reference: must be reclaimed by GC during GC
Virtual references: because virtual references are only used to know whether an object is GC or not
one。 Strong reference (StrongReference)
Strong references are the most commonly used references. If an object has a strong reference, the garbage collector will never reclaim it. As follows:
Object strongReference = new Object ()
When the memory space is insufficient, the Java virtual machine would rather throw an OutOfMemoryError error to cause the program to terminate abnormally, rather than solve the problem of insufficient memory by randomly collecting objects with strong references. If the strongly referenced object is not in use, it needs to be weakened so that GC can be recycled, as follows:
StrongReference = null
If you explicitly set the strongReference object to null, or make it out of the lifecycle of the object, gc believes that there is no reference to the object, and the object can be recycled. Exactly when to collect depends on the GC algorithm.
two。 Soft reference (SoftReference)
If an object has only soft references, the garbage collector will not reclaim it when there is enough memory space; if there is not enough memory space, it will reclaim the memory of those objects. As long as the garbage collector does not reclaim it, the object can be used by the program.
Soft references can be used to implement memory-sensitive caching.
/ / soft reference String str = new String ("abc"); SoftReference softReference = new SoftReference (str)
When memory runs out, JVM first sets the object reference in the soft reference to null, and then notifies the garbage collector to recycle:
If (out of JVM memory) {/ / set the object reference in the soft reference to null str = null; / / notify the garbage collector to recycle System.gc ();}
That is, the garbage collection thread reclaims soft reference objects before the virtual machine throws the OutOfMemoryError, and the virtual machine reclaims soft reference objects that have been idle for a long time as much as possible. "newer" soft objects that have just been built or just used will be retained by the virtual machine as much as possible, which is why the reference queue ReferenceQueue is introduced.
Application scenarios:
The back button of the browser. When you press back, will the content of the page displayed when you press back be re-requested or taken out of the cache? It depends on the specific implementation strategy.
If a web page recycles its content at the end of browsing, it needs to be rebuilt when you press back to view the previously viewed page
If the browsed web pages are stored in memory, it will cause a lot of memory waste, and even cause memory overflow.
At this time, soft references can be used to solve the practical problems:
/ / get the browser object to browse Browser browser = new Browser (); / / load the browsing page BrowserPage page = browser.getPage () from the background program; / / set the browsed page to a soft reference SoftReference softReference = new SoftReference (page) / / when you roll back or browse this page again, if (softReference.get ()! = null) {/ / has sufficient memory and has not been reclaimed by the collector, so directly get the cache page = softReference.get ();} else {/ / insufficient memory, the soft-referenced object has been recycled page = browser.getPage () / / rebuild the soft reference softReference = new SoftReference (page);} three. Weak reference (WeakReference)
The difference between weak references and soft references is that objects with only weak references have a shorter life cycle. When the garbage collector thread scans the memory area under its jurisdiction, once an object with only weak references is found, its memory will be reclaimed regardless of whether the current memory space is sufficient or not. However, because the garbage collector is a low-priority thread, objects with only weak references will not necessarily be found quickly.
String str = new String ("abc"); WeakReference weakReference = new WeakReference (str); str = null
If an object is used occasionally (rarely) and you want to get it at any time when you use it, but do not want to affect the object's garbage collection, then you should use Weak Reference to remember the object. An example of using weak references is WeakHashMap, which is another implementation of the Map interface in addition to HashMap and TreeMap. WeakHashMap has a feature: the keys in map are encapsulated as weak references, that is, once the strong reference is removed, the weak reference within the WeakHashMap cannot prevent the object from being reclaimed by the garbage collector.
The following code turns a weak reference into a strong reference again:
String str = new String ("abc"); WeakReference weakReference = new WeakReference (str); / / weak reference to strong reference String strongReference = weakReference.get ()
The member variables of ThreadLocalMap in Threadlocal are stored in the WeakReference array inside ThreadLocalMap, and the key of the array is the hash value within ThreadLocal.
four。 Virtual reference (PhantomReference)
False quotation, as the name implies, is in vain. Unlike several other references, virtual references do not determine the life cycle of an object. If an object holds only a virtual reference, it can be reclaimed by the garbage collector at any time as if it had no reference at all.
Application scenarios:
Virtual references are mainly used to track the activity of objects being reclaimed by the garbage collector. One of the differences between virtual references and soft and weak references is:
Virtual references must be used in conjunction with reference queues (ReferenceQueue). When the garbage collector is ready to recycle an object, if it finds that it has a virtual reference, it will add the virtual reference to the reference queue associated with it before reclaiming the object's memory.
String str = new String ("abc"); ReferenceQueue queue = new ReferenceQueue (); / / create a virtual reference, which requires that PhantomReference pr = new PhantomReference (str, queue) must be associated with a reference queue
The program can know whether the referenced object is going 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 memory of the referenced object is reclaimed.
This is the end of the summary of citation knowledge points in Java. Thank you for your 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.