In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the memory leak caused by ThreadLocal". In the daily operation, I believe many people have doubts about how to understand the memory leak caused by ThreadLocal. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to understand the memory leak caused by ThreadLocal"! Next, please follow the editor to study!
Demand background
A to C personal account system, the amount of data is about more than 2000 W; one day the product suddenly wants to do a to B merchant account system, immediately after a week or so to promote activities will be used, the product has been blown out with the big boss, it is said that our system already has this ability and can support seamlessly immediately. Now we are in a hurry to come to us and ask if we can directly provide the capacity of our to C account system.
Demand analysis
First of all, to develop and test online in such a short time, it must be unrealistic to make a new to B account system. No matter how strong our programmer's ability is, we can't make an assembly line production system. We can only rely on the ability of the current account system to support the business. The core domain models at the bottom of these two sets of account systems can be abstractly consistent, including accounting vouchers, bookkeeping subjects, and bookkeeping pipelining. The difference is that the entry and exit rules are different in different business usage scenarios, so the domain model of the rule layer needs customized configuration development. At the same time, the system already has more than 2000 W accounts, the daily increase pipeline is also 10W, the amount of data itself is already very large, and the two sets of data in the business will affect each other, so it is necessary to do vertical table in the business and isolate the two sets of data.
Implementation process
The domain model and data storage scheme were implemented as soon as they were settled. For a more elegant implementation, it had the least impact on the current to C business. We deliberately intercept at the interface entrance and add a judgment tag to the threadlocal of the current worker thread to identify whether it is the business request of to C or the business request of to B. then the business rule layer filters through different filter. Finally, different tables are processed by different tags in the jdbc layer of the database. The program was implemented very quickly, and the test was completed in two or three days, but two problems were found in the testing process. the first problem is that the data is sometimes confused for no reason, and the data originally written to the to B table structure will be written into the to C table structure. And during the pressure test, it was found that the memory monitoring curve was growing slowly, and a memory leak occurred.
Analysis of problems
After careful analysis of the source code, it was found that the problem occurred in threadlocal. There is a ThreadLocalMap data structure in each thread, and when the set method is executed, its value is stored in the threadLocals variable of the current thread, and when the get method is executed, it is obtained from the threadLocals variable of the current thread. Therefore, the value of set in thread 1 is invisible to thread 2, and re-set in thread 2 will not affect the value in thread 1, ensuring that threads will not interfere with each other.
Public void set (T value) {Thread t = Thread.currentThread (); ThreadLocalMap map = getMap (t); if (map! = null) map.set (this, value); else createMap (t, value);} public T get () {Thread t = Thread.currentThread (); ThreadLocalMap map = getMap (t); if (map! = null) {ThreadLocalMap.Entry e = map.getEntry (this) If (e! = null) {@ SuppressWarnings ("unchecked") T result = (T) e.value; return result;}} return setInitialValue ();} ThreadLocalMap getMap (Thread t) {return t.threadLocals;} / / Entry is the ThreadLocalMap static inner class, if the reference to ThreadLocal / / makes ThreadLocal and stored values form the key-value relationship static class Entry extends WeakReference at the same time
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.