Top Description Inners Fields Constructors Methods
com.fasterxml.jackson.core.util

pack-priv Class ThreadLocalBufferManager

extends Object
Class Inheritance
Imports
java.lang.ref.ReferenceQueue, .SoftReference, java.util.*, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.locks.ReentrantLock

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 otherwise ClassLoader would have dangling reference via ThreadLocals. 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 and Inner Type Summary

Modifier and TypeClass and 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

Modifier and TypeField and Description
private final ReferenceQueue<BufferRecycler>
_refQueue

Queue where gc will put just-cleared SoftReferences, previously referencing BufferRecyclers.

private final Map<SoftReference<BufferRecycler>, Boolean>
_trackedRecyclers

A set of all SoftReferences to all BufferRecyclers to be able to release them on shutdown.

private final ReentrantLock
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

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public static ThreadLocalBufferManager
instance()

Returns the lazily initialized singleton instance

public int
releaseBuffers()

Releases the buffers retained in ThreadLocals.

private void
removeSoftRefsClearedByGc()

Remove cleared (inactive) SoftRefs from our set.

public SoftReference<BufferRecycler>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

_refQueueback to summary
private final 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.

_trackedRecyclersback to summary
private final Map<SoftReference<BufferRecycler>, 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 its equals() and hashCode() implementations defined so that they use object identity, so we do not need to use something like IdentityHashMap

RELEASE_LOCKback to summary
private final ReentrantLock 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 Detail

ThreadLocalBufferManagerback to summary
pack-priv ThreadLocalBufferManager()

Method Detail

instanceback to summary
public static ThreadLocalBufferManager instance()

Returns the lazily initialized singleton instance

releaseBuffersback to summary
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.

removeSoftRefsClearedByGcback to summary
private void removeSoftRefsClearedByGc()

Remove cleared (inactive) SoftRefs from our set. Gc may have cleared one or more, and made them inactive.

wrapAndTrackback to summary
public SoftReference<BufferRecycler> wrapAndTrack(BufferRecycler br)
com.fasterxml.jackson.core.util back to summary

private final Class ThreadLocalBufferManager.ThreadLocalBufferManagerHolder

extends Object
Class Inheritance
  • java.lang.Object
  • com.fasterxml.jackson.core.util.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

Modifier and TypeField and Description
pack-priv static final ThreadLocalBufferManager

Constructor Summary

AccessConstructor and Description
private

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

managerback to summary
pack-priv static final ThreadLocalBufferManager manager

Constructor Detail

ThreadLocalBufferManagerHolderback to summary
private ThreadLocalBufferManagerHolder()