Cache the objects frequently used to reduce memory churn. The ByteArray should be held onto as long as the data referenced in it is needed.
For small arrays where the management of valid bytes in ByteArray and prezeroing isn't required, use SimpleByteArray instead.
Heap size control - survey of usage:
+---------------------------------------------------------------------+ | Size | Max | MaxMem | From | +---------------------------------------------------------------------+ | 1K | 32 | 32K | tunnel TrivialPreprocessor | | 1K | 512 | 512K | tunnel FragmentHandler | | 1K | 512 | 512K | tunnel I2NP TunnelDataMessage | | 1K | 512 | 512K | tunnel FragmentedMessage | | 1572 | 64 | 100K | UDP InboundMessageState | | 1730 | 128 | 216K | streaming MessageOutputStream | | 4K | 32 | 28K | I2PTunnelRunner | | 8K | 8 | 64K | I2PTunnel HTTPResponseOutputStream | | 16K | 16 | 256K | I2PSnark | | 32K | 4 | 128K | SAM StreamSession | | 32K | 10 | 320K | SAM v2StreamSession | | 32K | 64 | 2M | UDP OMS | | 32K | 128 | 4M | streaming MessageInputStream | | 36K | 64 | 2.25M | streaming PacketQueue | | 40K | 8 | 320K | DataHelper decompress | | 64K | 64 | 4M | UDP MessageReceiver | +---------------------------------------------------------------------+
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classFactory for cached byte arrays.private static classGlobal cleanup task that iterates over all caches.Nested classes/interfaces inherited from class TryCache
TryCache.ObjectFactory<T>Modifier and TypeClassDescriptionstatic interfaceFactory interface for creating new objects in TryCache.Field Summary
FieldsModifier and TypeFieldDescriptionGlobal cleanup task - single timer for all caches.The caches, by entry size.private final intThe size of the entries in this cache.private static final LogThe log for the byte cache.private static final intHow often do we cleanup all caches.private static final longIf the cache size hasn't been exceeded in 90 seconds, cut the cache in half.private static final intLargest entry size actually allowed.private static final intLargest entry size that may be cached.private static final intMax size in bytes of each cache Set to max memory / 128, with a min of 256KB and a max of 8MB.private static final intFewest objects a cache holds.Constructor Summary
ConstructorsModifierConstructorDescriptionprivateByteCache(int maxCachedEntries, int entrySize) Caches byte arrays of one size.Method Summary
Modifier and TypeMethodDescriptionprivate voidcleanup()Cleanup this specific cache - shrink if underutilized.static voidclearAll()Clear everything (memory pressure).static ByteCachegetInstance(int cacheSize, int size) Shared cache for the given entry size and capacity.final voidPut this structure back onto the available cache for reuse.final voidRelease the entry back to the cache, optionally zeroing its contents first.voidresize(int maxCachedEntries) Resize the cache to a new capacity.Methods inherited from class TryCache
acquire, clear, getAcquireCount, getCapacity, getDiscardCount, getLastUnderflowTime, getMissCount, getReleaseCount, shrink, size, wasUnderfilledModifier and TypeMethodDescriptionacquire()Acquire an object from the cache or create a new one.voidclear()Clear all cached items.longReturn total acquire() count (hits + misses).intReturn the current cache capacity.longReturn the number ofTryCache.release(T)calls that were discarded because the cache was full.longWhen the cache last fell below its target size.longReturn the number ofTryCache.acquire()calls that found an empty cache (misses required allocating a new object).longReturn total release() count (returned to cache).voidshrink(int targetSize) Shrink the cache to the target size, evicting excess items.intsize()Number of items currently held.booleanwasUnderfilled(long thresholdMillis) Check if the cache has been underfilled longer than the threshold.
Field Details
_log
The log for the byte cache._caches
MIN_CACHE
private static final int MIN_CACHEMax size in bytes of each cache Set to max memory / 128, with a min of 256KB and a max of 8MB.- See Also:
MAX_CACHE_LIMIT
private static final int MAX_CACHE_LIMITLargest entry size that may be cached.- See Also:
MAX_CACHE
private static final int MAX_CACHELargest entry size actually allowed.MIN_CACHE_OBJECTS
private static final int MIN_CACHE_OBJECTSFewest objects a cache holds.- 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 the cache size hasn't been exceeded in 90 seconds, cut the cache in half.- See Also:
_allCaches
_entrySize
private final int _entrySizeThe size of the entries in this cache.
Constructor Details
ByteCache
private ByteCache(int maxCachedEntries, int entrySize) Caches byte arrays of one size.- Parameters:
maxCachedEntries- the most arrays to cacheentrySize- the size of the arrays
Method Details
cleanup
private void cleanup()Cleanup this specific cache - shrink if underutilized.getInstance
Shared cache for the given entry size and capacity.- Parameters:
cacheSize- the number of entries to hold, trimmed as needed to stay under MAX_CACHEsize- the byte length of every cached entry, raised to at least 1 and used as the cache key- Returns:
- the shared cache for that entry size, creating it if needed
clearAll
public static void clearAll()Clear everything (memory pressure).resize
release
release
Release the entry back to the cache, optionally zeroing its contents first.- Parameters:
entry- the structure being handed back, ignored if null or the wrong sizeshouldZero- true to overwrite the entry's bytes with zeroes before pooling it