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
Modifier and TypeFieldDescriptionstatic final int
The size of buffers that are returned. -
Method Summary
Modifier and TypeMethodDescriptionstatic long
Gets the number ofbyte[]
buffers instantiated.static long
Gets the number ofbyte[]
buffers detected to have been garbage collected.static long
Gets the number of timebyte[]
buffers have been used.static long
Gets the number of timebyte[]
buffers have been zero-filled on release.static byte[]
getBytes()
Gets abyte[]
of lengthBUFFER_SIZE
that may be temporarily used for any purpose.static long
Gets the number ofchar[]
buffers instantiated.static long
Gets the number ofchar[]
buffers detected to have been garbage collected.static long
Gets the number of timechar[]
buffers have been used.static long
Gets the number of timechar[]
buffers have been zero-filled on release.static char[]
getChars()
Gets achar[]
of lengthBUFFER_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 abyte[]
that was obtained by a call togetBytes
.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 achar[]
that was obtained by a call togetChars
.
-
Field Details
-
BUFFER_SIZE
public static final int BUFFER_SIZEThe size of buffers that are returned.- See Also:
-
-
Method Details
-
getBytes
public static byte[] getBytes()Gets abyte[]
of lengthBUFFER_SIZE
that may be temporarily used for any purpose. Once done with the buffer,release
should be called, this is best accomplished in afinally
block. The buffer is not necessarily zero-filled and may contain data from a previous use. -
getChars
public static char[] getChars()Gets achar[]
of lengthBUFFER_SIZE
that may be temporarily used for any purpose. Once done with the buffer,release
should be called, this is best accomplished in afinally
block. The buffer is not necessarily zero-filled and may contain data from a previous use. -
release
Deprecated.May obtain greater performance by avoiding zero fill on non-sensitive data. -
release
public static void release(byte[] buffer, boolean zeroFill) Releases abyte[]
that was obtained by a call togetBytes
. A buffer must not be released more than once.- Parameters:
buffer
- thebyte[]
to releasezeroFill
- if the data in the buffer may be sensitive, it is best to zero-fill the buffer on release.
-
release
Deprecated.May obtain greater performance by avoiding zero fill on non-sensitive data. -
release
public static void release(char[] buffer, boolean zeroFill) Releases achar[]
that was obtained by a call togetChars
. A buffer must not be released more than once.- Parameters:
buffer
- thechar[]
to releasezeroFill
- 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 ofbyte[]
buffers instantiated. -
getByteBufferUses
public static long getByteBufferUses()Gets the number of timebyte[]
buffers have been used. -
getByteBufferZeroFills
public static long getByteBufferZeroFills()Gets the number of timebyte[]
buffers have been zero-filled on release. -
getByteBuffersCollected
public static long getByteBuffersCollected()Gets the number ofbyte[]
buffers detected to have been garbage collected. -
getCharBufferCreates
public static long getCharBufferCreates()Gets the number ofchar[]
buffers instantiated. -
getCharBufferUses
public static long getCharBufferUses()Gets the number of timechar[]
buffers have been used. -
getCharBufferZeroFills
public static long getCharBufferZeroFills()Gets the number of timechar[]
buffers have been zero-filled on release. -
getCharBuffersCollected
public static long getCharBuffersCollected()Gets the number ofchar[]
buffers detected to have been garbage collected.
-