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.
| Modifier and Type | Class 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 |
| Modifier and Type | Field and Description |
|---|---|
| private final ReferenceQueue | _refQueue
Queue where gc will put just-cleared SoftReferences, previously referencing BufferRecyclers. |
| private final Map | _trackedRecyclers
A set of all SoftReferences to all BufferRecyclers to be able to release them on shutdown. |
| private final 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. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public static ThreadLocalBufferManager | |
| public int | |
| private void | |
| public SoftReference |
| _refQueue | back 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. | |
| _trackedRecyclers | back 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 | |
| RELEASE_LOCK | back to summary |
|---|---|
| private final 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. | |
| ThreadLocalBufferManager | back to summary |
|---|---|
| pack-priv ThreadLocalBufferManager() | |
| instance | back to summary |
|---|---|
| public static ThreadLocalBufferManager instance() Returns the lazily initialized singleton instance | |
| releaseBuffers | back 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. | |
| removeSoftRefsClearedByGc | back to summary |
|---|---|
| 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 | |
| wrapAndTrack | back to summary |
|---|---|
| public SoftReference | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final ThreadLocalBufferManager |
| Access | Constructor and Description |
|---|---|
| private |
| manager | back to summary |
|---|---|
| pack-priv static final ThreadLocalBufferManager manager | |
| ThreadLocalBufferManagerHolder | back to summary |
|---|---|
| private ThreadLocalBufferManagerHolder() | |