This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

What is a daemon thread in Java?

Java. Lang. Ref. What is the difference between a WeakReference and Java. Lang. Ref. SoftReference?

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

From Understanding Weak Citations by Ethan Nicholas:

Weak references

A weak reference, in short, is not strong enough to force the object to remain in memory as a reference. Weak references allow you to take advantage of the garbage collector’s ability to determine your reachability, so you don’t have to do it yourself. You create weak references like this :(no strong references are recycled)

WeakReference weakWidget = new WeakReference(widget);
Copy the code

Then, elsewhere in the code, you can use WeakWidget.get () to get the actual Widget object. Of course, weak references are not enough to prevent garbage collection, so you might find that weakWidget.get() suddenly starts returning NULL if there is no strong reference to the widget.

Soft references

A soft reference is like a weak reference, except that it is less eager to throw away the object it references. Only weakly reachable objects (the strongest reference to which is WeakReferences) are discarded in the next garbage collection cycle, but weakly reachable objects usually stay around for a while.

SoftReferences do not need to be reserved to any objects that behave differently than WeakReferences, but are generally gently reachable in practice, as long as memory is adequately supplied. This makes them a good base for caches, such as the image cache above, because you can make the garbage collector worry that object accessibility (a highly accessible object will never be removed from the cache) requires the memory they are consuming.

Peter Kessler added in his comments:

Sun JRE does treat SoftReference and WeakReferences differently. If the available memory is not stressed, we try to preserve the object referenced by SoftReference. One detail: the “-client” and “-server” JRE have different policies: the -client JRE tends to reduce footprint by clearing SoftReferences rather than expanding the heap, while the -server JRE tries to preserve your footprint. Performance can be improved by prioritizing extending the heap (if possible) rather than clearing SoftReferences. One size does not fit all.

The article translated from am2dgbqfb6mk75jcyanzabc67y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 2…

If a Threadlocal key does not have a strong reference, it will be collected by the garbage collector. As a result, the key will be empty and the value will still have data. Therefore, in order to avoid memory leakage, it is recommended to release the remove in time


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️