threadLocalMap Review

threadLocal和threadLocalMap

Uses: Add to threadlocalMap object, the same thread thread can be anywhere out

understanding:

Each thread thead object has a threadLocalMap object may have a plurality of threadLocal objects

threadLocalMap class is threadLocal static inner class class belongs threadLocal class, and not to threadLocal objects

In threadLocal methods in the class to create threadLoalMap objects

threadLocalMap element object of the entry of key is threadLocal target, value data needs to be stored, and may have a plurality of threadLocal objects

entry The key is a weak reference ThreadLocal objects (weak reference objects to survive until the next garbage collection occurs before), so before the garbage collector clears the entry object key, appears key to null the entry, but the value does not share memory will be released, memory leaks may occur (not free up memory space has been filed), eventually leading to memory overflow (no memory can be allocated to a new object). In ThreadLocal class GET (), SET (), Remove () method has implemented the recovery  key is  null,  the Entry  value memory occupied. Therefore, in order to prevent memory leaks, when the task will not use the thread end ThreadLocal object, call the three methods once one of the above can be. In fact, personal understanding will reclaim all the memory when the thread dies.

// Note that in the web, because tomcat thread reuse, if the user information exists threadLocalMap object, the request must be emptied after the end of

Attachment:

Strong references:  can not be recovered memory.

Soft references:  when sufficient recovery of the internal memory.

Weak references:  survival into memory before garbage collection.

Guess you like

Origin www.cnblogs.com/csuliujia/p/12566344.html