The principle of ThreadLocal and the cause of memory leak

        Before understanding the principle of ThradLocal, it is necessary to understand the reference type of java, because the underlying layer of Threadlocal uses weak references, you can pass this knowledge by the way, and this is also a common question during interviews        

1. Java reference type:

        Reference types are strong, soft, weak, and virtual

2. Let’s talk about the characteristics of these reference types

         Strong reference: The object directly new in java belongs to a strong reference. If an object has a strong reference, the JVM will not recycle it. When the memory space is not enough, an OOM error will be reported.

        

        Soft reference: If an object has only soft references, then when the memory space is not enough, the JVM will recycle the object (Soft Reference )
       

        Weak reference: If an object only has a weak reference, it is detected by the gc thread of the jvm, and the object will be recycled (Weak Reference)
        

        Phantom reference: directly manages local memory

3.ThradLocaL

        ThreadLocL is a local variable of the thread. This variable can bind data to the current thread in the case of multi-threading. This data cannot be read and modified by other threads, thus ensuring the safety of the thread.

4. The underlying principle of the set method

             When ThreadLocL calls the set method, the bottom layer of the set method is to obtain the current thread first, and then use the getmap method to obtain the ThreadLocLs variable of the current thread, then use ThradLocL as the key, and the set method to pass in the parameter as the value, through ThreadLocLs The set method is saved. When saving, the key will be saved in the form of a weak reference, and the value will be saved in the form of a strong reference.

 

        So when Threadlocl calls the set method to save the value, it is not saved in Threadlocl, but in the ThreadLocLs variable of the current thread (the type of this variable is ThreadLocalMap), and this variable is a map structure. The key saves the ThreadLocL reference that calls the set method, and the value is the value that needs to be saved

5. The underlying principle of the get method

        When ThreadLocL calls the get method, it will first obtain the current thread, and then use the getmap method to obtain the ThreadLocLs variable of the current thread. Through the getEntry method of the variable, the parameter is ThreadLocL, and the saved data can be obtained

6. Why should the key be set as a weak reference, but is it not possible to use a strong reference?

       Using strong references may cause memory leaks. When a thread no longer needs to use a certain Threadlocl, if the key is a strong reference type, then jvm cannot recycle it. If the thread keeps starting, it will take a long time and there will be Oom errors may be reported, but weak application types will not, as long as the jvm performs a gc operation, it will be recycled

7. Why should the value be set as a strong reference, can't weak references work?

        If the value is a weak reference, as long as the jvm performs a gc operation, the value will be recycled, and null will be returned when the value is retrieved, so it can only be a strong reference

8. Memory leaks

        The first case: if the key is recycled by the gc of the jvm, then the value value cannot be accessed. If the thread is running all the time, it may cause an oom error. Although the set and get methods of ThreadLocLMap will be adjusted. The Entry object whose key is null is deleted, but if the thread has not called these two methods for a long time, it cannot be recycled, so we need to manually remove after use to ensure the safety of the code
 

        The second case: If the thread pool is used for thread multiplexing, after the thread is used, if the remove operation is not performed, the thread's ThreadloclMap will save more and more values, and the value of the value may be overwritten. , so manual remove operations are also required to ensure code security

9. Why is the key in ThreadLocalMap.Entry not cleared by mistake?

         When an object has only weak references pointing to it, it will be recycled once the jvm executes the gc operation, but the ThreadLocal object not only has weak references in the Entry, but also has strong references in the business code, so it will not be wrong to clean up

10. The use of ThreadLocal in business scenarios

        Store user information globally

 

 

Guess you like

Origin blog.csdn.net/qq_26112725/article/details/129782931