Class PooledMemoryManager

  • All Implemented Interfaces:
    MemoryManager<Buffer>, WrapperAware, MonitoringAware<MemoryProbe>

    public class PooledMemoryManager
    extends java.lang.Object
    implements MemoryManager<Buffer>, WrapperAware
    A MemoryManager implementation based on a series of shared memory pools. Each pool contains multiple buffers of the fixed length specific for this pool. There are several tuning options for this MemoryManager implementation.
    • The base size of the buffer for the 1st pool, every next pool n will have buffer size equal to bufferSize(n-1) * 2^growthFactor
    • The number of pools, responsible for allocation of buffers of a pool-specific size
    • The buffer size growth factor, that defines 2^x multiplier, used to calculate buffer size for next allocated pool
    • The number of pool slices that every pool will stripe allocation requests across
    • The percentage of the heap that this manager will use when populating the pools
    • The percentage of buffers to be pre-allocated during MemoryManager initialization
    • The flag indicating whether direct or heap based Buffers will be allocated
    If no explicit configuration is provided, the following defaults will be used: The main advantage of this manager over HeapMemoryManager or ByteBufferManager is that this implementation doesn't use ThreadLocal pools and as such, doesn't suffer from the memory fragmentation/reallocation cycle that can impact the ThreadLocal versions.
    Since:
    2.3.11
    • Constructor Detail

      • PooledMemoryManager

        public PooledMemoryManager()
        Creates a new PooledMemoryManager using the following defaults:
        • 4 KiB base buffer size
        • 3 pools
        • 2 growth factor, which means 1st pool will contain buffers of size 4KiB, the 2nd - 16KiB, the 3rd - 64KiB
        • Number of pool slices based on Runtime.getRuntime().availableProcessors()
        • The initial allocation will use 3% of the heap
        • The percentage of buffers to be pre-allocated during MemoryManager initialization
      • PooledMemoryManager

        public PooledMemoryManager​(boolean isDirect)
        Creates a new PooledMemoryManager using the specified parameters for configuration.
        Parameters:
        isDirect - flag, indicating whether direct or heap based Buffers will be allocated
      • PooledMemoryManager

        public PooledMemoryManager​(int baseBufferSize,
                                   int numberOfPools,
                                   int growthFactor,
                                   int numberOfPoolSlices,
                                   float percentOfHeap,
                                   float percentPreallocated,
                                   boolean isDirect)
        Creates a new PooledMemoryManager using the specified parameters for configuration.
        Parameters:
        baseBufferSize - the base size of the buffer for the 1st pool, every next pool n will have buffer size equal to bufferSize(n-1) * 2^growthFactor
        numberOfPools - the number of pools, responsible for allocation of buffers of a pool-specific size
        growthFactor - the buffer size growth factor, that defines 2^x multiplier, used to calculate buffer size for next allocated pool
        numberOfPoolSlices - the number of pool slices that every pool will stripe allocation requests across
        percentOfHeap - percentage of the heap that will be used when populating the pools
        percentPreallocated - percentage of buffers to be pre-allocated during MemoryManager initialization
        isDirect - flag, indicating whether direct or heap based Buffers will be allocated
    • Method Detail

      • allocateAtLeast

        public Buffer allocateAtLeast​(int size)
        Allocates a buffer of at least the size requested.

        Keep in mind that the capacity of the buffer may be greater than the allocation request. The limit however, will be set to the specified size. The memory beyond the limit, is available for use.

        Specified by:
        allocateAtLeast in interface MemoryManager<Buffer>
        Parameters:
        size - the min Buffer size to be allocated.
        Returns:
        a buffer with a limit of the specified size.
      • reallocate

        public Buffer reallocate​(Buffer oldBuffer,
                                 int newSize)
        Reallocates an existing buffer to at least the specified size.
        Specified by:
        reallocate in interface MemoryManager<Buffer>
        Parameters:
        oldBuffer - old Buffer to be reallocated.
        newSize - new Buffer required size.
        Returns:
        potentially a new buffer of at least the specified size.
      • release

        public void release​(Buffer buffer)
        Release Buffer. Implementation may ignore releasing and let JVM Garbage collector to take care about the Buffer, or return Buffer to pool, in case of more complex MemoryManager implementation.
        Specified by:
        release in interface MemoryManager<Buffer>
        Parameters:
        buffer - Buffer to be released.
      • wrap

        public Buffer wrap​(byte[] data)
        Description copied from interface: WrapperAware
        Returns Buffer, which wraps the byte array.
        Specified by:
        wrap in interface WrapperAware
        Parameters:
        data - byte array to wrap
        Returns:
        Buffer wrapper on top of passed byte array.
      • wrap

        public Buffer wrap​(byte[] data,
                           int offset,
                           int length)
        Description copied from interface: WrapperAware
        Returns Buffer, which wraps the part of byte array with specific offset and length.
        Specified by:
        wrap in interface WrapperAware
        Parameters:
        data - byte array to wrap
        offset - byte buffer offset
        length - byte buffer length
        Returns:
        Buffer wrapper on top of passed byte array.
      • wrap

        public Buffer wrap​(java.lang.String s)
        Description copied from interface: WrapperAware
        Returns Buffer, which wraps the String.
        Specified by:
        wrap in interface WrapperAware
        Parameters:
        s - String
        Returns:
        Buffer wrapper on top of passed String.
      • wrap

        public Buffer wrap​(java.lang.String s,
                           java.nio.charset.Charset charset)
        Description copied from interface: WrapperAware
        Returns Buffer, which wraps the String with the specific Charset.
        Specified by:
        wrap in interface WrapperAware
        Parameters:
        s - String
        charset - Charset, which will be used, when converting String to byte array.
        Returns:
        Buffer wrapper on top of passed String.
      • wrap

        public Buffer wrap​(java.nio.ByteBuffer byteBuffer)
        Description copied from interface: WrapperAware
        Returns Buffer, which wraps the ByteBuffer.
        Specified by:
        wrap in interface WrapperAware
        Parameters:
        byteBuffer - ByteBuffer to wrap
        Returns:
        Buffer wrapper on top of passed ByteBuffer.
      • createJmxManagementObject

        protected java.lang.Object createJmxManagementObject()