Package com.lucimber.dbus.util
Class ByteBufferPool
java.lang.Object
com.lucimber.dbus.util.ByteBufferPool
A thread-safe ByteBuffer pool to reduce garbage collection pressure by reusing ByteBuffer
instances for encoding/decoding operations.
This pool maintains separate queues for different buffer sizes, automatically sizing up to the nearest power of two to improve reuse efficiency. Buffers are cleaned before being returned to ensure data integrity.
The pool has built-in size limits to prevent unbounded memory growth under high load scenarios.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAcquires a ByteBuffer with at least the requested capacity.void
clear()
Clears all pooled buffers, releasing memory back to the system.Gets statistics about the current pool state.void
release
(ByteBuffer buffer) Returns a ByteBuffer to the pool for potential reuse.
-
Constructor Details
-
ByteBufferPool
public ByteBufferPool()Creates a new ByteBuffer pool instance.
-
-
Method Details
-
acquire
Acquires a ByteBuffer with at least the requested capacity. The returned buffer will have its position at 0, limit at capacity, and will be cleared of any previous data.- Parameters:
capacity
- minimum required capacityorder
- byte order for the buffer- Returns:
- a ByteBuffer ready for use
-
release
Returns a ByteBuffer to the pool for potential reuse. The buffer should not be used after calling this method.- Parameters:
buffer
- the buffer to return (may be null)
-
getStatistics
Gets statistics about the current pool state. Useful for monitoring and debugging memory usage.- Returns:
- a string representation of pool statistics
-
clear
public void clear()Clears all pooled buffers, releasing memory back to the system. Useful for cleanup or testing scenarios.
-