In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to prevent ThreadLocal memory leaks". In daily operation, I believe many people have doubts about how to prevent ThreadLocal memory leaks. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to prevent ThreadLocal memory leaks". Next, please follow the editor to study!
ThreadLocal memory leaks and weak references
1. What is a memory leak? The relationship between key weak reference and leakage of Entry
A memory leak in TreadLocal means that key in Entry in TreadLocalMap is null, while value is not null. Because key is null, value cannot be accessed all the time, and according to reachability analysis, there is always threadRef- > currentThread- > threadLocalMap- > entry- > valueRef- > valueMemory, so that when the reachability analysis is performed during garbage collection, the value is reachable and will not be recycled, but the value can never be accessed, so there is a memory leak.
Because the key of Entry is a weak reference, key will be recycled during gc, while value is a strong reference, so value will not be recycled.
If weak references are not used, memory leaks may occur. As long as the reference of ThreadLocal is set to null in the business code, the value in Entry will not be accessed, but because it is reachable, the gc will not be recycled, which means that this part of memory resources are wasted.
two。 Why Entry's key uses weak references
Suppose that threadLocal uses strong references and performs threadLocal Instance=null operations in the business code to clean up the threadLocal instance. However, because the Entry of threadLocalMap strongly references threadLocal, threadLocal is still reachable during gc, and there is no garbage collection for threadLocal. As a result, the purpose of business logic cannot be really achieved, and logic errors occur.
Suppose that Entry weakly references threadLocal, although there will be memory leaks, but in the life cycle of threadLocal (set,getEntry,remove), dirty entry whose key is null will be dealt with.
3. Prevent memory leaks
In fact, a lot of optimizations have been made to the memory leak problem in the ThreadLocal source code. In the set,get,remove method, the value null operation is not carried out for the Entry whose key is null but value is not for null, so that the reference of value is null, and the reachability fails. Value memory can be reclaimed in gc.
In daily use, when you finally run out of TreadLocal, remember remove. Why?
Because without remove, when a gc is executed, the value will cause a memory leak until the end of the current thread (when the thread ends, the ThreaLocalMap will be set to null, and the Entry in ThreaLocalMap itself will not be reachable, will be recycled, everything will be recycled)
The Thread.exit method is executed at the end of the thread
Private void exit () {if (group! = null) {group.threadTerminated (this); group = null;} / * Aggressively null out all reference fields: see bug 4006245 * / target = null; / * Speed the release of some of these resources * / threadLocals = null; inheritableThreadLocals = null; inheritedAccessControlContext = null; blocker = null; uncaughtExceptionHandler = null;} Anonymous inner classes cause memory leaks
Memory leak: objects that should have been reclaimed by GC cannot be recycled due to various reasons, resulting in a waste of memory resources, resulting in OOM.
If a class uses an inner class, and the life cycle of the two classes is different, for example, the life cycle of the inner class is longer than that of the outer class.
This will lead to the end of the life cycle of the external class, which should have been recycled, but because the inner class will implicitly strongly reference the external class, the external class cannot be recycled.
Resulting in a memory leak.
Solution
1. You can avoid using inner classes
two。 Inner classes can use weak references to refer to external classes
3. Using static inner classes, static inner classes do not hold references to external classes (weak references can be used if you want to call external class methods or use external class properties).
At this point, the study on "how to prevent ThreadLocal memory leaks" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 239
*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.