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
AMemoryManager
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 thisMemoryManager
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
Buffer
s 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
Buffer
s will be allocated
HeapMemoryManager
orByteBufferManager
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
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_BASE_BUFFER_SIZE
static int
DEFAULT_GROWTH_FACTOR
static float
DEFAULT_HEAP_USAGE_PERCENTAGE
static int
DEFAULT_NUMBER_OF_POOLS
static float
DEFAULT_PREALLOCATED_BUFFERS_PERCENTAGE
protected DefaultMonitoringConfig<MemoryProbe>
monitoringConfig
Basic monitoring support.-
Fields inherited from interface org.glassfish.grizzly.memory.MemoryManager
DEFAULT_MEMORY_MANAGER
-
-
Constructor Summary
Constructors Constructor Description PooledMemoryManager()
Creates a newPooledMemoryManager
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 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 newPooledMemoryManager
using the specified parameters for configuration.PooledMemoryManager(int baseBufferSize, int numberOfPools, int growthFactor, int numberOfPoolSlices, float percentOfHeap, float percentPreallocated, boolean isDirect)
Creates a newPooledMemoryManager
using the specified parameters for configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Buffer
allocate(int size)
For this implementation, this method simply calls through toallocateAtLeast(int)
;Buffer
allocateAtLeast(int size)
Allocates a buffer of at least the size requested.protected Object
createJmxManagementObject()
MonitoringConfig<MemoryProbe>
getMonitoringConfig()
Return the object associatedMonitoringConfig
.Buffer
reallocate(Buffer oldBuffer, int newSize)
Reallocates an existing buffer to at least the specified size.void
release(Buffer buffer)
ReleaseBuffer
.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 aBuffer
based on directByteBuffer
, or false otherwise.Buffer
wrap(byte[] data)
ReturnsBuffer
, which wraps the byte array.Buffer
wrap(byte[] data, int offset, int length)
ReturnsBuffer
, which wraps the part of byte array with specific offset and length.Buffer
wrap(String s)
Buffer
wrap(String s, Charset charset)
Buffer
wrap(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 newPooledMemoryManager
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 newPooledMemoryManager
using the specified parameters for configuration.- Parameters:
isDirect
- flag, indicating whether direct or heap basedBuffer
s will be allocated
-
PooledMemoryManager
public PooledMemoryManager(int baseBufferSize, int numberOfPools, int growthFactor, int numberOfPoolSlices, float percentOfHeap, float percentPreallocated, boolean isDirect)
Creates a newPooledMemoryManager
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^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 basedBuffer
s will be allocated
-
-
Method Detail
-
allocate
public Buffer allocate(int size)
For this implementation, this method simply calls through toallocateAtLeast(int)
;- Specified by:
allocate
in interfaceMemoryManager<Buffer>
- Parameters:
size
-Buffer
size 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:
allocateAtLeast
in interfaceMemoryManager<Buffer>
- Parameters:
size
- the minBuffer
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 interfaceMemoryManager<Buffer>
- Parameters:
oldBuffer
- oldBuffer
to be reallocated.newSize
- newBuffer
required 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 returnBuffer
to pool, in case of more complex MemoryManager implementation.- Specified by:
release
in interfaceMemoryManager<Buffer>
- Parameters:
buffer
-Buffer
to 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 aBuffer
based on directByteBuffer
, or false otherwise.- Specified by:
willAllocateDirect
in interfaceMemoryManager<Buffer>
- Returns:
-
getMonitoringConfig
public MonitoringConfig<MemoryProbe> getMonitoringConfig()
Return the object associatedMonitoringConfig
.- Specified by:
getMonitoringConfig
in interfaceMonitoringAware<MemoryProbe>
- Returns:
- the object associated
MonitoringConfig
.
-
wrap
public Buffer wrap(byte[] data)
Description copied from interface:WrapperAware
ReturnsBuffer
, which wraps the byte array.- Specified by:
wrap
in interfaceWrapperAware
- 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
ReturnsBuffer
, which wraps the part of byte array with specific offset and length.- Specified by:
wrap
in interfaceWrapperAware
- Parameters:
data
- byte array to wrapoffset
- byte buffer offsetlength
- byte buffer length- Returns:
Buffer
wrapper on top of passed byte array.
-
wrap
public Buffer wrap(String s)
Description copied from interface:WrapperAware
- Specified by:
wrap
in interfaceWrapperAware
- Parameters:
s
-String
- Returns:
Buffer
wrapper 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:WrapperAware
ReturnsBuffer
, which wraps theByteBuffer
.- Specified by:
wrap
in interfaceWrapperAware
- Parameters:
byteBuffer
-ByteBuffer
to wrap- Returns:
Buffer
wrapper on top of passedByteBuffer
.
-
createJmxManagementObject
protected Object createJmxManagementObject()
-
-