java.lang.Object
net.i2p.util.SimpleByteCache

public final class SimpleByteCacheextends Object
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 Classes
    Modifier and Type
    Class
    Description
    private static class 
    Factory for cached byte arrays.
    private static class 
    Global cleanup task that iterates over all caches.
    private static final class 
    A set of SimpleByteCache, keyed by array size.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final TryCache<byte[]>
    The buffers available for reuse.
    private static final SimpleByteCache.IntCacheMap
    The caches, keyed by array size.
    private final int
    Size of each cached buffer.
    private static final int
    How often do we cleanup all caches.
    private static final int
    Default number of buffers cached.
    private static final long
    If we haven't exceeded the cache size in 90 seconds, cut our cache in half.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    SimpleByteCache(int maxCachedEntries, int entrySize)
    Builds a cache of byte arrays.
  • Method Summary

    Modifier and Type
    Method
    Description
    private byte[]
    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 int
    Current object count ceiling.
    private void
    Cleanup this specific cache - shrink if underutilized.
    private void
    Clear everything (memory pressure).
    static void
    Clear everything (memory pressure).
    getInstance(int size)
    Cache responsible for arrays of the given size, using the default size.
    getInstance(int cacheSize, int size)
    Cache responsible for objects of the given size.
    static void
    release(byte[] entry)
    Put this array back onto the available cache for reuse.
    private void
    releaseIt(byte[] entry)
    Put this array back onto the available cache for reuse.
    private void
    resize(int maxCachedEntries)
    Apply a new object count to the existing cache.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • _caches

      private static final SimpleByteCache.IntCacheMap _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_SIZE
      Default number of buffers cached.
      See Also:
    • CLEANUP_FREQUENCY

      private static final int CLEANUP_FREQUENCY
      How often do we cleanup all caches.
      See Also:
    • EXPIRE_PERIOD

      private static final long EXPIRE_PERIOD
      If we haven't exceeded the cache size in 90 seconds, cut our cache in half.
      See Also:
    • _available

      private final TryCache<byte[]> _available
      The buffers available for reuse.
    • _entrySize

      private final int _entrySize
      Size of each cached buffer.
  • Constructor Details

    • SimpleByteCache

      private SimpleByteCache(int maxCachedEntries, int entrySize)
      Builds a cache of byte arrays.
      Parameters:
      maxCachedEntries - most buffers cached
      entrySize - the size of each buffer
  • Method Details

    • cleanup

      private void cleanup()
      Cleanup this specific cache - shrink if underutilized.
    • getInstance

      public static SimpleByteCache getInstance(int size)
      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 size bytes
    • getInstance

      public static SimpleByteCache getInstance(int cacheSize, int size)
      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 objects
      size - how large should the objects cached be?
      Returns:
      the cache for arrays of size bytes, 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).