org.glassfish.grizzly.memory
Class HeapMemoryManager

java.lang.Object
  extended by org.glassfish.grizzly.memory.AbstractMemoryManager<HeapBuffer>
      extended by org.glassfish.grizzly.memory.HeapMemoryManager
All Implemented Interfaces:
MemoryManager<HeapBuffer>, ThreadLocalPoolProvider, WrapperAware, JmxMonitoringAware<MemoryProbe>, MonitoringAware<MemoryProbe>

public class HeapMemoryManager
extends AbstractMemoryManager<HeapBuffer>
implements WrapperAware

A WrapperAware MemoryManager implementation for managing HeapBuffer instances.

Since:
2.0

Nested Class Summary
 
Nested classes/interfaces inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
AbstractMemoryManager.TrimAware
 
Field Summary
 
Fields inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
DEFAULT_MAX_BUFFER_SIZE, maxBufferSize, monitoringConfig
 
Fields inherited from interface org.glassfish.grizzly.memory.MemoryManager
DEFAULT_MEMORY_MANAGER
 
Constructor Summary
HeapMemoryManager()
           
HeapMemoryManager(int maxBufferSize)
           
 
Method Summary
 HeapBuffer allocate(int size)
          Allocated Buffer of the required size.
 HeapBuffer allocateAtLeast(int size)
          Allocated Buffer at least of the provided size.
protected  HeapBuffer allocateHeapBuffer(int size)
           
protected  HeapBuffer allocateHeapBufferAtLeast(int size)
           
protected  JmxObject createJmxManagementObject()
          
 ThreadLocalPool createThreadLocalPool()
          
 JmxMonitoringConfig<MemoryProbe> getMonitoringConfig()
          Return the object associated JmxMonitoringConfig.
 HeapBuffer reallocate(HeapBuffer oldBuffer, int newSize)
          Reallocate Buffer to a required size.
protected  HeapBuffer reallocateHeapBuffer(HeapBuffer oldHeapBuffer, int newSize)
           
 void release(HeapBuffer buffer)
          Release Buffer.
protected  void releaseHeapBuffer(HeapBuffer heapBuffer)
           
 boolean willAllocateDirect(int size)
          Return true if next MemoryManager.allocate(int) or MemoryManager.allocateAtLeast(int) call, made in the current thread for the given memory size, going to return a Buffer based on direct ByteBuffer, or false otherwise.
 HeapBuffer wrap(byte[] data)
          Returns Buffer, which wraps the byte array.
 HeapBuffer wrap(byte[] data, int offset, int length)
          Returns Buffer, which wraps the part of byte array with specific offset and length.
 Buffer wrap(ByteBuffer byteBuffer)
          Returns Buffer, which wraps the ByteBuffer.
 HeapBuffer wrap(String s)
          Returns Buffer, which wraps the String.
 HeapBuffer wrap(String s, Charset charset)
          Returns Buffer, which wraps the String with the specific Charset.
 
Methods inherited from class org.glassfish.grizzly.memory.AbstractMemoryManager
allocateFromPool, getMaxBufferSize, getReadyThreadBufferSize, getThreadLocalPool
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HeapMemoryManager

public HeapMemoryManager()

HeapMemoryManager

public HeapMemoryManager(int maxBufferSize)
Method Detail

allocate

public HeapBuffer allocate(int size)
Allocated Buffer of the required size.

Specified by:
allocate in interface MemoryManager<HeapBuffer>
Parameters:
size - Buffer size to be allocated.
Returns:
allocated Buffer.

allocateAtLeast

public HeapBuffer allocateAtLeast(int size)
Allocated Buffer at least of the provided size. This could be useful for usecases like Socket.read(...), where we're not sure how many bytes are available, but want to read as much as possible.

Specified by:
allocateAtLeast in interface MemoryManager<HeapBuffer>
Parameters:
size - the min Buffer size to be allocated.
Returns:
allocated Buffer.

reallocate

public HeapBuffer reallocate(HeapBuffer oldBuffer,
                             int newSize)
Reallocate Buffer to a required size. Implementation may choose the way, how reallocation could be done, either by allocating new Buffer of required size and copying old Buffer content there, or perform more complex logic related to memory pooling etc.

Specified by:
reallocate in interface MemoryManager<HeapBuffer>
Parameters:
oldBuffer - old Buffer to be reallocated.
newSize - new Buffer required size.
Returns:
reallocated Buffer.

release

public void release(HeapBuffer 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<HeapBuffer>
Parameters:
buffer - Buffer to be released.

willAllocateDirect

public boolean willAllocateDirect(int size)
Return true if next MemoryManager.allocate(int) or MemoryManager.allocateAtLeast(int) call, made in the current thread for the given memory size, going to return a Buffer based on direct ByteBuffer, or false otherwise.

Specified by:
willAllocateDirect in interface MemoryManager<HeapBuffer>
Returns:

getMonitoringConfig

public JmxMonitoringConfig<MemoryProbe> getMonitoringConfig()
Return the object associated JmxMonitoringConfig.

Specified by:
getMonitoringConfig in interface JmxMonitoringAware<MemoryProbe>
Specified by:
getMonitoringConfig in interface MonitoringAware<MemoryProbe>
Returns:
the object associated JmxMonitoringConfig.

createThreadLocalPool

public ThreadLocalPool createThreadLocalPool()

Specified by:
createThreadLocalPool in interface ThreadLocalPoolProvider
Returns:
a new ThreadLocalPool implementation. This method must return a new ThreadLocalPool instance per invocation.

createJmxManagementObject

protected JmxObject createJmxManagementObject()

Specified by:
createJmxManagementObject in class AbstractMemoryManager<HeapBuffer>
Returns:
the JmxObject used to register/deregister with the JMX runtime.

wrap

public HeapBuffer wrap(byte[] data)
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 HeapBuffer wrap(byte[] data,
                       int offset,
                       int length)
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 HeapBuffer wrap(String s)
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 HeapBuffer wrap(String s,
                       Charset charset)
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(ByteBuffer byteBuffer)
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.

allocateHeapBuffer

protected HeapBuffer allocateHeapBuffer(int size)

allocateHeapBufferAtLeast

protected HeapBuffer allocateHeapBufferAtLeast(int size)

reallocateHeapBuffer

protected HeapBuffer reallocateHeapBuffer(HeapBuffer oldHeapBuffer,
                                          int newSize)

releaseHeapBuffer

protected final void releaseHeapBuffer(HeapBuffer heapBuffer)


Copyright © 2012 Oracle Corporation. All Rights Reserved.