In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is the principle of ThreadLocal and what is the cause of memory leak? aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
ThreadLocal has two problems:
1. Where is each copy of the variable stored?
two。 How are copies of variables assigned from shared variables? When the initial value of threadlocal in the source code is set.
Does ThreadLocal maintain a copy of a variable for each thread?
The ThreadLocalMap of each thread is held by the thread itself, but the initialization is in the ThreadLocal, and then each thread is equivalent to saving a map
The key stored in this map is an instance of LocalThread, and value is the local variable of the stored thread.
Get method
Get the value in Thread based on the current thread
Set method
Get the map of the current thread according to the current thread, and then set the value of the current thread
Thus ensuring that the value of each thread is obtained from its own ThreadMap
Why does ThreadLocal cause memory leaks
The weak reference of ThreadLocal is used as key. If gc wants to recycle, ThreadLcoal must be recycled. The strong reference Thread ref-> Thread-> ThreadLocalMap- > Entry- > value will always exist, resulting in memory leakage.
The answer to the question about the principle of ThreadLocal and the cause of memory leak is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.