Threadlocal If the analysis of memory leaks will be triggered

Source: Internet
Author: User

This article, the main solution to the doubts: 1. The weak references mentioned in Threadlocal.threadlocalmap, will the weak references be recycled? 2. What is the case for weak reference recycling? 3. Java threadlocal and under what circumstances will the memory leak? With these questions, I simulated the structure of the Threadlocal.threadlocalmap, first show the structure of their own: the realization of a simple threadlocalmap, Inside, a entry is used to store the value of the set method set in the threadlocal called by itself. And the same as the JDK Threadlocalmap inside the entry object's key in WeakReference package. The main method is as follows:
Set run parameters:-VERBOSE:GC look at the output: Here I have simulated a memory leak problem, you can see the full GC after the memory is still occupied, and careful observation can see that the key in this map is already null. In other words, you can't get the value through key, of course map.get (null) is also possible, but Java threadlocal does not do so, because the key==null element in map may not be unique. As you can see from my main method, I have a th=null operation, but there is still a memory leak, the reason for later analysis. But one thing is certain: th=null here can not be as we imagined the threadlocal th reference release, the inside of the Key,value object is also released, there may be doubts I have here to hold the Threadlocalmap reference TM so will not be recycled, but in fact,  When you manually set the Java threadlocal to null, the current line Cheng Jinghan holds the Threadlocalmap reference, so it is not recycled I am here and Java is similar. Back to the first 3 questions, one answer: 1.     The weak references mentioned in Threadlocal.threadlocalmap, will the weak references be recycled? will be recycled as shown in. Key already has a null condition. The first key is not NULL, the reason is 2nd. All keys have been recycled after the full GC has been experienced.     2. What is the case for weak reference recycling?     When a weak reference is scanned into a GC (including MINITORGC and full GC), it is recycled, but there is a precondition that the weak reference is not referenced externally (at this point the external reference is equal to the strong reference). In other words, if I hold a reference to a key in my main method, it will not be recycled even if it is set to a weak reference after it is put in the map. See:


GC log:
3. Java threadlocal and under what circumstances will the memory leak? The answer is no, for example, when we call Threadlocal.set (), we do a work to clean up the key== null element, in the following way: The first step: Threadlocalmap     Take the threadlocalhashcode and the length minus one phase, and find out the location of the hash table I. Step Two: entry, if the key is found equal, overwrite the original value!     Or find the Key==null, set the value in, and set the traversal of the key==null element and his value to null, freeing the memory. The third step: when the last if condition, do rehash action, namely: the element in the entry to recalculate the hash value, put in the appropriate position, conjecture is to speed up the next visit.

Summary: From this point of view, Java threadlocal to the key using a weak reference, but in order to ensure that no longer memory leaks, in each set.get time to actively key==null entry do the traversal of the recovery.     Although it does not cause a memory leak, but because the entry does not make a key==null judgment on each set,get, freeing up memory, it may cause large objects to survive in memory for a long time, thus occupying memory.     Therefore, it is better to remove the object manually after we have finished using threadlocal, or at least to invoke the next threadlocal.set (null). It is important to note that the key in ThreadLocal is currently the current ThreadLocal itself, like the above simulation of the external holding a strong reference case, the key==null situation in threadlocal.threadlocalmap rarely occur, because, In most cases, threadlocal is always present in singleton mode.

    • Size: 18.7 KB
    • Size: 15.9 KB
    • Size: 26.8 KB
    • Size: 26.4 KB
    • Size: 39.3 KB
    • Size: 26.2 KB
    • Weakreferencetest.rar (7.6 KB)
http://liuinsect.iteye.com/blog/1827012

Threadlocal If a memory leak is caused by the analysis (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.