java.lang.Object
net.i2p.util.SimpleByteCache
Like ByteCache but works directly with byte arrays, not ByteArrays. These are designed to be small caches. Unlike ByteCache, the arrays are not zeroed before being returned to the cache, so a caller that needs cleanup must zero the array itself.
A single global cleanup task runs once a minute over all caches and halves any cache that has been underfilled for 90 seconds, so the array sizes of an idle program are not retained indefinitely.
Only the static methods are public here.
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classFactory for cached byte arrays.private static classGlobal cleanup task that iterates over all caches.private static final classA set ofSimpleByteCache, keyed by array size.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final TryCache<byte[]> The buffers available for reuse.private static final SimpleByteCache.IntCacheMapThe caches, keyed by array size.private final intSize of each cached buffer.private static final intHow often do we cleanup all caches.private static final intDefault number of buffers cached.private static final longIf we haven't exceeded the cache size in 90 seconds, cut our cache in half.Constructor Summary
ConstructorsModifierConstructorDescriptionprivateSimpleByteCache(int maxCachedEntries, int entrySize) Builds a cache of byte arrays.Method Summary
Modifier and TypeMethodDescriptionprivate byte[]acquire()Next available array, either from the cache or a brand new one.static byte[]acquire(int size) Next available array, either from the cache or a brand new one.private intcapacity()Current object count ceiling.private voidcleanup()Cleanup this specific cache - shrink if underutilized.private voidclear()Clear everything (memory pressure).static voidclearAll()Clear everything (memory pressure).static SimpleByteCachegetInstance(int size) Cache responsible for arrays of the given size, using the default size.static SimpleByteCachegetInstance(int cacheSize, int size) Cache responsible for objects of the given size.static voidrelease(byte[] entry) Put this array back onto the available cache for reuse.private voidreleaseIt(byte[] entry) Put this array back onto the available cache for reuse.private voidresize(int maxCachedEntries) Apply a new object count to the existing cache.
Field Details
_caches
The caches, keyed by array size. An int-keyed table rather than a ConcurrentHashMap<Integer, SimpleByteCache>, so the hot acquire and release lookups neither hash an Integer nor box the size.DEFAULT_SIZE
private static final int DEFAULT_SIZEDefault number of buffers cached.- See Also:
CLEANUP_FREQUENCY
private static final int CLEANUP_FREQUENCYHow often do we cleanup all caches.- See Also:
EXPIRE_PERIOD
private static final long EXPIRE_PERIODIf we haven't exceeded the cache size in 90 seconds, cut our cache in half.- See Also:
_available
The buffers available for reuse._entrySize
private final int _entrySizeSize of each cached buffer.
Constructor Details
SimpleByteCache
private SimpleByteCache(int maxCachedEntries, int entrySize) Builds a cache of byte arrays.- Parameters:
maxCachedEntries- most buffers cachedentrySize- the size of each buffer
Method Details
cleanup
private void cleanup()Cleanup this specific cache - shrink if underutilized.getInstance
Cache responsible for arrays of the given size, using the default size.- Parameters:
size- how large should the objects cached be?- Returns:
- the shared cache holding arrays of
sizebytes
getInstance
Cache responsible for objects of the given size.There is one cache per size for the life of the JVM, and cacheSize is (re)applied to it whenever it differs from the size the cache already holds: a later call with a different cacheSize resizes the shared cache, discarding the excess on a shrink. The arrays evicted are simply garbage collected - nothing zeroes them.
- Parameters:
cacheSize- how many objects (NOT memory bytes) to keep before discarding released objectssize- how large should the objects cached be?- Returns:
- the cache for arrays of
sizebytes, shared by every caller that asked for the same size
clearAll
public static void clearAll()Clear everything (memory pressure).resize
private void resize(int maxCachedEntries) Apply a new object count to the existing cache. Shrinking evicts the excess immediately; growing only raises the ceiling for future releases. Concurrent acquire/release calls are unaffected.- Parameters:
maxCachedEntries- the new maximum number of entries
capacity
private int capacity()Current object count ceiling.- Returns:
- the maximum number of entries currently held
acquire
public static byte[] acquire(int size) Next available array, either from the cache or a brand new one.- Parameters:
size- how large should the object be?- Returns:
- a byte array of exactly that length, taken from the cache when one of that size is free and allocated otherwise; never null
acquire
private byte[] acquire()Next available array, either from the cache or a brand new one.- Returns:
- an array of this cache's entry size, never null
release
public static void release(byte[] entry) Put this array back onto the available cache for reuse.- Parameters:
entry- the array to recycle; ignored if null, or if no cache exists for its length
releaseIt
private void releaseIt(byte[] entry) Put this array back onto the available cache for reuse.- Parameters:
entry- the array to recycle; ignored if its length does not match this cache's entry size
clear
private void clear()Clear everything (memory pressure).