Package org.glassfish.grizzly.memory
Class PooledMemoryManager
- java.lang.Object
-
- org.glassfish.grizzly.memory.PooledMemoryManager
-
- All Implemented Interfaces:
MemoryManager<Buffer>,WrapperAware,MonitoringAware<MemoryProbe>
public class PooledMemoryManager extends Object implements MemoryManager<Buffer>, WrapperAware
AMemoryManagerimplementation 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 thisMemoryManagerimplementation.- 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
- Base buffer size: 4 KiB (
DEFAULT_BASE_BUFFER_SIZE) - Number of pools: 3 (
DEFAULT_NUMBER_OF_POOLS) - Growth factor: 2 (
DEFAULT_GROWTH_FACTOR), which means the first buffer pool will contains buffer of size 4 KiB, the seconds one buffer of size 16KiB, the third one buffer of size 64KiB - Number of pool slices: Based on the return value of
Runtime.getRuntime().availableProcessors() - Percentage of heap: 3% (
DEFAULT_HEAP_USAGE_PERCENTAGE) - Percentage of buffers to be pre-allocated: 100% (
DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE) - Heap based
Buffers will be allocated
HeapMemoryManagerorByteBufferManageris 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
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_BASE_BUFFER_SIZEstatic intDEFAULT_GROWTH_FACTORstatic floatDEFAULT_HEAP_USAGE_PERCENTAGEstatic intDEFAULT_NUMBER_OF_POOLSstatic floatDEFAULT_PREALLOCATED_BUFFERS_PERCENTAGEprotected DefaultMonitoringConfig<MemoryProbe>monitoringConfigBasic monitoring support.-
Fields inherited from interface org.glassfish.grizzly.memory.MemoryManager
DEFAULT_MEMORY_MANAGER
-
-
Constructor Summary
Constructors Constructor Description PooledMemoryManager()Creates a newPooledMemoryManagerusing 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 onRuntime.getRuntime().availableProcessors()The initial allocation will use 3% of the heap The percentage of buffers to be pre-allocated during MemoryManager initializationPooledMemoryManager(boolean isDirect)Creates a newPooledMemoryManagerusing the specified parameters for configuration.PooledMemoryManager(int baseBufferSize, int numberOfPools, int growthFactor, int numberOfPoolSlices, float percentOfHeap, float percentPreallocated, boolean isDirect)Creates a newPooledMemoryManagerusing the specified parameters for configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Bufferallocate(int size)For this implementation, this method simply calls through toallocateAtLeast(int);BufferallocateAtLeast(int size)Allocates a buffer of at least the size requested.protected ObjectcreateJmxManagementObject()MonitoringConfig<MemoryProbe>getMonitoringConfig()Return the object associatedMonitoringConfig.Bufferreallocate(Buffer oldBuffer, int newSize)Reallocates an existing buffer to at least the specified size.voidrelease(Buffer buffer)ReleaseBuffer.booleanwillAllocateDirect(int size)Return true if nextMemoryManager.allocate(int)orMemoryManager.allocateAtLeast(int)call, made in the current thread for the given memory size, going to return aBufferbased on directByteBuffer, or false otherwise.Bufferwrap(byte[] data)ReturnsBuffer, which wraps the byte array.Bufferwrap(byte[] data, int offset, int length)ReturnsBuffer, which wraps the part of byte array with specific offset and length.Bufferwrap(String s)Bufferwrap(String s, Charset charset)Bufferwrap(ByteBuffer byteBuffer)ReturnsBuffer, which wraps theByteBuffer.
-
-
-
Field Detail
-
DEFAULT_BASE_BUFFER_SIZE
public static final int DEFAULT_BASE_BUFFER_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_NUMBER_OF_POOLS
public static final int DEFAULT_NUMBER_OF_POOLS
- See Also:
- Constant Field Values
-
DEFAULT_GROWTH_FACTOR
public static final int DEFAULT_GROWTH_FACTOR
- See Also:
- Constant Field Values
-
DEFAULT_HEAP_USAGE_PERCENTAGE
public static final float DEFAULT_HEAP_USAGE_PERCENTAGE
- See Also:
- Constant Field Values
-
DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE
public static final float DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE
- See Also:
- Constant Field Values
-
monitoringConfig
protected final DefaultMonitoringConfig<MemoryProbe> monitoringConfig
Basic monitoring support. Concrete implementations of this class need only to implement thecreateJmxManagementObject()method to plug into the Grizzly 2.0 JMX framework.
-
-
Constructor Detail
-
PooledMemoryManager
public PooledMemoryManager()
Creates a newPooledMemoryManagerusing 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 newPooledMemoryManagerusing the specified parameters for configuration.- Parameters:
isDirect- flag, indicating whether direct or heap basedBuffers will be allocated
-
PooledMemoryManager
public PooledMemoryManager(int baseBufferSize, int numberOfPools, int growthFactor, int numberOfPoolSlices, float percentOfHeap, float percentPreallocated, boolean isDirect)Creates a newPooledMemoryManagerusing 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^growthFactornumberOfPools- the number of pools, responsible for allocation of buffers of a pool-specific sizegrowthFactor- the buffer size growth factor, that defines 2^x multiplier, used to calculate buffer size for next allocated poolnumberOfPoolSlices- the number of pool slices that every pool will stripe allocation requests acrosspercentOfHeap- percentage of the heap that will be used when populating the poolspercentPreallocated- percentage of buffers to be pre-allocated during MemoryManager initializationisDirect- flag, indicating whether direct or heap basedBuffers will be allocated
-
-
Method Detail
-
allocate
public Buffer allocate(int size)
For this implementation, this method simply calls through toallocateAtLeast(int);- Specified by:
allocatein interfaceMemoryManager<Buffer>- Parameters:
size-Buffersize to be allocated.- Returns:
- allocated
Buffer.
-
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:
allocateAtLeastin interfaceMemoryManager<Buffer>- Parameters:
size- the minBuffersize 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:
reallocatein interfaceMemoryManager<Buffer>- Parameters:
oldBuffer- oldBufferto be reallocated.newSize- newBufferrequired size.- Returns:
- potentially a new buffer of at least the specified size.
-
release
public void release(Buffer buffer)
ReleaseBuffer. Implementation may ignore releasing and let JVM Garbage collector to take care about theBuffer, or returnBufferto pool, in case of more complex MemoryManager implementation.- Specified by:
releasein interfaceMemoryManager<Buffer>- Parameters:
buffer-Bufferto be released.
-
willAllocateDirect
public boolean willAllocateDirect(int size)
Return true if nextMemoryManager.allocate(int)orMemoryManager.allocateAtLeast(int)call, made in the current thread for the given memory size, going to return aBufferbased on directByteBuffer, or false otherwise.- Specified by:
willAllocateDirectin interfaceMemoryManager<Buffer>- Returns:
-
getMonitoringConfig
public MonitoringConfig<MemoryProbe> getMonitoringConfig()
Return the object associatedMonitoringConfig.- Specified by:
getMonitoringConfigin interfaceMonitoringAware<MemoryProbe>- Returns:
- the object associated
MonitoringConfig.
-
wrap
public Buffer wrap(byte[] data)
Description copied from interface:WrapperAwareReturnsBuffer, which wraps the byte array.- Specified by:
wrapin interfaceWrapperAware- Parameters:
data- byte array to wrap- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
public Buffer wrap(byte[] data, int offset, int length)
Description copied from interface:WrapperAwareReturnsBuffer, which wraps the part of byte array with specific offset and length.- Specified by:
wrapin interfaceWrapperAware- Parameters:
data- byte array to wrapoffset- byte buffer offsetlength- byte buffer length- Returns:
Bufferwrapper on top of passed byte array.
-
wrap
public Buffer wrap(String s)
Description copied from interface:WrapperAware- Specified by:
wrapin interfaceWrapperAware- Parameters:
s-String- Returns:
Bufferwrapper on top of passedString.
-
wrap
public Buffer wrap(String s, Charset charset)
Description copied from interface:WrapperAware
-
wrap
public Buffer wrap(ByteBuffer byteBuffer)
Description copied from interface:WrapperAwareReturnsBuffer, which wraps theByteBuffer.- Specified by:
wrapin interfaceWrapperAware- Parameters:
byteBuffer-ByteBufferto wrap- Returns:
Bufferwrapper on top of passedByteBuffer.
-
createJmxManagementObject
protected Object createJmxManagementObject()
-
-