Package com.fasterxml.jackson.core.util
Class ThreadLocalBufferManager
- java.lang.Object
-
- com.fasterxml.jackson.core.util.ThreadLocalBufferManager
-
class ThreadLocalBufferManager extends java.lang.Object
For issue [jackson-core#400] We keep a separate Set of all SoftReferences to BufferRecyclers which are (also) referenced using `ThreadLocals`. We do this to be able to release them (dereference) in `releaseBuffers()` and `shutdown()` method to reduce heap consumption during hot reloading of services where otherwiseClassLoader
would have dangling reference viaThreadLocal
s. When gc clears a SoftReference, it puts it on a newly introduced referenceQueue. We use this queue to release the inactive SoftReferences from the Set.- Since:
- 2.9.6
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ThreadLocalBufferManager.ThreadLocalBufferManagerHolder
ThreadLocalBufferManagerHolder uses the thread-safe initialize-on-demand, holder class idiom that implicitly incorporates lazy initialization by declaring a static variable within a static Holder inner class
-
Field Summary
Fields Modifier and Type Field Description private java.lang.ref.ReferenceQueue<BufferRecycler>
_refQueue
Queue where gc will put just-cleared SoftReferences, previously referencing BufferRecyclers.private java.util.Map<java.lang.ref.SoftReference<BufferRecycler>,java.lang.Boolean>
_trackedRecyclers
A set of all SoftReferences to all BufferRecyclers to be able to release them on shutdown.private java.lang.Object
RELEASE_LOCK
A lock to make sure releaseBuffers is only executed by one thread at a time since it iterates over and modifies the allSoftBufRecyclers.
-
Constructor Summary
Constructors Constructor Description ThreadLocalBufferManager()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ThreadLocalBufferManager
instance()
Returns the lazily initialized singleton instanceint
releaseBuffers()
Releases the buffers retained in ThreadLocals.private void
removeSoftRefsClearedByGc()
Remove cleared (inactive) SoftRefs from our set.java.lang.ref.SoftReference<BufferRecycler>
wrapAndTrack(BufferRecycler br)
-
-
-
Field Detail
-
RELEASE_LOCK
private final java.lang.Object RELEASE_LOCK
A lock to make sure releaseBuffers is only executed by one thread at a time since it iterates over and modifies the allSoftBufRecyclers.
-
_trackedRecyclers
private final java.util.Map<java.lang.ref.SoftReference<BufferRecycler>,java.lang.Boolean> _trackedRecyclers
A set of all SoftReferences to all BufferRecyclers to be able to release them on shutdown. 'All' means the ones created by this class, in this classloader. There may be more from other classloaders. We use a HashSet to have quick O(1) add and remove operations.NOTE: assumption is that
SoftReference
has itsequals()
andhashCode()
implementations defined so that they use object identity, so we do not need to use something likeIdentityHashMap
-
_refQueue
private final java.lang.ref.ReferenceQueue<BufferRecycler> _refQueue
Queue where gc will put just-cleared SoftReferences, previously referencing BufferRecyclers. We use it to remove the cleared softRefs from the above set.
-
-
Method Detail
-
instance
public static ThreadLocalBufferManager instance()
Returns the lazily initialized singleton instance
-
releaseBuffers
public int releaseBuffers()
Releases the buffers retained in ThreadLocals. To be called for instance on shutdown event of applications which make use of an environment like an appserver which stays alive and uses a thread pool that causes ThreadLocals created by the application to survive much longer than the application itself. It will clear all bufRecyclers from the SoftRefs and release all SoftRefs itself from our set.
-
wrapAndTrack
public java.lang.ref.SoftReference<BufferRecycler> wrapAndTrack(BufferRecycler br)
-
removeSoftRefsClearedByGc
private void removeSoftRefsClearedByGc()
Remove cleared (inactive) SoftRefs from our set. Gc may have cleared one or more, and made them inactive. We minimize contention by keeping synchronized sections short: the poll/remove methods
-
-