Class BufferManager


  • public final class BufferManager
    extends Object

    BufferManager manages a reusable pool of byte[] and char[] buffers. This avoids the repetitive allocation of memory for an operation that only needs a temporary buffer. The buffers are stored as ThreadLocal to maximize cache locality.

    Do not use if intra-thread security is more important than performance.

    The buffers are not necessarily cleared between invocations so the results of previous operations may be available to additional callers. On the scale of security versus performance, this is biased toward performance. However, being thread local there remains some control over the visibility of the data.

    Buffers should not be passed between threads. Giving a thread a buffer you didn't get from it could result in a memory or information leak. Soft references are used to avoid full-on memory leaks, but keeping buffers to a single thread is optimal.

    Under no circumstances should a buffer be released more than once. This may result in the buffer being allocated twice at the same time, with resulting data corruption.

    The Java virtual machine has improved greatly over the years. However, we still believe this buffer management to be valuable to reduce garbage collection pressure. If this ever proves to not be the case, the implementation here can be simply changed to create new arrays on each use.

    Author:
    AO Industries, Inc.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BUFFER_SIZE
      The size of buffers that are returned.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static long getByteBufferCreates()
      Gets the number of byte[] buffers instantiated.
      static long getByteBuffersCollected()
      Gets the number of byte[] buffers detected to have been garbage collected.
      static long getByteBufferUses()
      Gets the number of time byte[] buffers have been used.
      static long getByteBufferZeroFills()
      Gets the number of time byte[] buffers have been zero-filled on release.
      static byte[] getBytes()
      Gets a byte[] of length BUFFER_SIZE that may be temporarily used for any purpose.
      static long getCharBufferCreates()
      Gets the number of char[] buffers instantiated.
      static long getCharBuffersCollected()
      Gets the number of char[] buffers detected to have been garbage collected.
      static long getCharBufferUses()
      Gets the number of time char[] buffers have been used.
      static long getCharBufferZeroFills()
      Gets the number of time char[] buffers have been zero-filled on release.
      static char[] getChars()
      Gets a char[] of length BUFFER_SIZE that may be temporarily used for any purpose.
      static void release​(byte[] buffer)
      Deprecated.
      May obtain greater performance by avoiding zero fill on non-sensitive data.
      static void release​(byte[] buffer, boolean zeroFill)
      Releases a byte[] that was obtained by a call to getBytes.
      static void release​(char[] buffer)
      Deprecated.
      May obtain greater performance by avoiding zero fill on non-sensitive data.
      static void release​(char[] buffer, boolean zeroFill)
      Releases a char[] that was obtained by a call to getChars.
    • Field Detail

      • BUFFER_SIZE

        public static final int BUFFER_SIZE
        The size of buffers that are returned.
        See Also:
        Constant Field Values
    • Method Detail

      • getBytes

        public static byte[] getBytes()
        Gets a byte[] of length BUFFER_SIZE that may be temporarily used for any purpose. Once done with the buffer, release should be called, this is best accomplished in a finally block. The buffer is not necessarily zero-filled and may contain data from a previous use.
      • getChars

        public static char[] getChars()
        Gets a char[] of length BUFFER_SIZE that may be temporarily used for any purpose. Once done with the buffer, release should be called, this is best accomplished in a finally block. The buffer is not necessarily zero-filled and may contain data from a previous use.
      • release

        @Deprecated
        public static void release​(byte[] buffer)
        Deprecated.
        May obtain greater performance by avoiding zero fill on non-sensitive data.
      • release

        public static void release​(byte[] buffer,
                                   boolean zeroFill)
        Releases a byte[] that was obtained by a call to getBytes. A buffer must not be released more than once.
        Parameters:
        buffer - the byte[] to release
        zeroFill - if the data in the buffer may be sensitive, it is best to zero-fill the buffer on release.
      • release

        @Deprecated
        public static void release​(char[] buffer)
        Deprecated.
        May obtain greater performance by avoiding zero fill on non-sensitive data.
      • release

        public static void release​(char[] buffer,
                                   boolean zeroFill)
        Releases a char[] that was obtained by a call to getChars. A buffer must not be released more than once.
        Parameters:
        buffer - the char[] to release
        zeroFill - if the data in the buffer may be sensitive, it is best to zero-fill the buffer on release.
      • getByteBufferCreates

        public static long getByteBufferCreates()
        Gets the number of byte[] buffers instantiated.
      • getByteBufferUses

        public static long getByteBufferUses()
        Gets the number of time byte[] buffers have been used.
      • getByteBufferZeroFills

        public static long getByteBufferZeroFills()
        Gets the number of time byte[] buffers have been zero-filled on release.
      • getByteBuffersCollected

        public static long getByteBuffersCollected()
        Gets the number of byte[] buffers detected to have been garbage collected.
      • getCharBufferCreates

        public static long getCharBufferCreates()
        Gets the number of char[] buffers instantiated.
      • getCharBufferUses

        public static long getCharBufferUses()
        Gets the number of time char[] buffers have been used.
      • getCharBufferZeroFills

        public static long getCharBufferZeroFills()
        Gets the number of time char[] buffers have been zero-filled on release.
      • getCharBuffersCollected

        public static long getCharBuffersCollected()
        Gets the number of char[] buffers detected to have been garbage collected.