In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "what is the cause of memory OOM caused by ThreadLocal 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!
Cause analysis
What is the cause of memory OOM caused by ThreadLocal?
The underlying layer of ThreadLocal stores data through ThreadLocalMap
The source code is as follows
When we use ThreadLocal.set (), the value and key of set (that is, the ThreadLocal class defined by the business itself) are stored in the Entry [] array of ThreadLocalMap
The source code is as follows
Where Entry is a key that implements a weak reference to WeakReference,Entry (that is, the ThreadLocal class defined by the business side) will be packaged as a weak reference as a key of Entry. Java's definition of weak reference is that when JVM performs a garbage collection scan, when an object with only a weak reference is found, it will be recycled immediately, which is a means to prevent memory overflow when ThreadLocal was originally designed.
The source code is as follows
Although key is packaged as a weak reference to be reclaimed by the garbage collection mechanism, value may have a strong reference chain when the thread (Thread) does not die.
Because value is a strong reference, as long as Thread does not die, such as thread pool, this strong reference chain will exist, then value will not be recycled, which may cause memory overflow
The reference relationship is as follows: Thread = > ThreadLocalMap = = > Entry = > value
However, this action to eliminate the strong reference chain needs to be triggered by the business side in the case of get. The business side may not get, or the get may be key is not empty, and the expungeStaleEntry class will not be triggered. So developers should form a good habit and remember to call the ThreadLocal.remove () method or ThreadLocal.set (null) once after using ThreadLocal.
Correct use of public class ThreadLocalTest {/ * uninitialized local thread variables * / private static ThreadLocal userThreadLocal = new ThreadLocal (); public static void main (String [] args) throws InterruptedException {int threadNum = 10; List threadList = Lists.newArrayList (); for (int I = 0; I
< threadNum; ++i) { long userId = i; Thread t = new Thread(() ->{try {/ / set the variable value userThreadLocal.set (new User (userId, "lanxing" + userId, "2x"); / / use the variable doSomething () } finally {/ / remove variable userThreadLocal.remove (); / / remove ThreadLocal variable}}, "T" + I); threadList.add (t); t.start ();} for (int I = 0; I < threadNum) + + I) {threadList.get (I) .join ();}} private static void doSomething () {log.info ("Use ThreadLocal variable: {}", JSON.toJSONString (userThreadLocal.get ();} @ Data@NoArgsConstructor@AllArgsConstructorclass User {private Long id; private String name; private String level;}
Print the results:
14 id 30 level 26.790 [T2] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 2, "level": "2x", "name": "lanxing2"}
14 id 30 level 26.789 [T5] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 5, "level": "2x", "name": "lanxing5"}
14 id 30 level 26.792 [T0] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 0, "level": "2x", "name": "lanxing0"}
14 id 30 level 26.792 [T4] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 4, "level": "2x", "name": "lanxing4"}
14 id 30 level 26.792 [T8] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 8, "level": "2x", "name": "lanxing8"}
14 id 30 level 26.791 [T1] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 1, "level": "2x", "name": "lanxing1"}
14 id 30 level 26.792 [T7] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 7, "level": "2x", "name": "lanxing7"}
14 id 30 level 26.792 [T6] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 6, "level": "2x", "name": "lanxing6"}
14 id 30 level 26.791 [T9] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 9, "level": "2x", "name": "lanxing9"}
14 id 30 level 26.790 [T3] INFO io.zhengsh.order.tool.ThreadLocalTest-Use ThreadLocal variable: {"id": 3, "level": "2x", "name": "lanxing3"}
This is the end of the content of "what is the cause of memory OOM caused by ThreadLocal in Java". 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.