public class MemoryManager extends Object
The memory may be represented as on-heap byte arrays (HeapMemorySegment
), or as off-heap
memory regions (HybridMemorySegment
). Which kind of memory the MemoryManager serves can
be passed as an argument to the initialization.
The memory manager can either pre-allocate all memory, or allocate the memory on demand. In the former version, memory will be occupied and reserved from start on, which means that no OutOfMemoryError can come while requesting memory. Released memory will also return to the MemoryManager's pool. On-demand allocation means that the memory manager only keeps track how many memory segments are currently allocated (bookkeeping only). Releasing a memory segment will not add it back to the pool, but make it re-claimable by the garbage collector.
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_PAGE_SIZE
The default memory page size.
|
static int |
MIN_PAGE_SIZE
The minimal memory page size.
|
Constructor and Description |
---|
MemoryManager(long memorySize,
int numberOfSlots)
Creates a memory manager with the given capacity, using the default page size.
|
MemoryManager(long memorySize,
int numberOfSlots,
int pageSize,
MemoryType memoryType,
boolean preAllocateMemory)
Creates a memory manager with the given capacity and given page size.
|
Modifier and Type | Method and Description |
---|---|
List<MemorySegment> |
allocatePages(Object owner,
int numPages)
Allocates a set of memory segments from this memory manager.
|
void |
allocatePages(Object owner,
List<MemorySegment> target,
int numPages)
Allocates a set of memory segments from this memory manager.
|
long |
computeMemorySize(double fraction)
Computes the memory size of the fraction per slot.
|
int |
computeNumberOfPages(double fraction)
Computes to how many pages the given number of bytes corresponds.
|
long |
getMemorySize()
Returns the total size of memory handled by this memory manager.
|
MemoryType |
getMemoryType()
Gets the type of memory (heap / off-heap) managed by this memory manager.
|
int |
getPageSize()
Gets the size of the pages handled by the memory manager.
|
int |
getTotalNumPages()
Gets the total number of memory pages managed by this memory manager.
|
boolean |
isPreAllocated()
Checks whether this memory manager pre-allocates the memory.
|
boolean |
isShutdown()
Checks whether the MemoryManager has been shut down.
|
void |
release(Collection<MemorySegment> segments)
Tries to release many memory segments together.
|
void |
release(MemorySegment segment)
Tries to release the memory for the specified segment.
|
void |
releaseAll(Object owner)
Releases all memory segments for the given owner.
|
long |
roundDownToPageSizeMultiple(long numBytes)
Rounds the given value down to a multiple of the memory manager's page size.
|
void |
shutdown()
Shuts the memory manager down, trying to release all the memory it managed.
|
boolean |
verifyEmpty()
Checks if the memory manager all memory available.
|
public static final int DEFAULT_PAGE_SIZE
public static final int MIN_PAGE_SIZE
public MemoryManager(long memorySize, int numberOfSlots)
memorySize
- The total size of the memory to be managed by this memory manager.numberOfSlots
- The number of slots of the task manager.public MemoryManager(long memorySize, int numberOfSlots, int pageSize, MemoryType memoryType, boolean preAllocateMemory)
memorySize
- The total size of the memory to be managed by this memory manager.numberOfSlots
- The number of slots of the task manager.pageSize
- The size of the pages handed out by the memory manager.memoryType
- The type of memory (heap / off-heap) that the memory manager should allocate.preAllocateMemory
- True, if the memory manager should immediately allocate all memory, false
if it should allocate and release the memory as needed.public void shutdown()
public boolean isShutdown()
public boolean verifyEmpty()
public List<MemorySegment> allocatePages(Object owner, int numPages) throws MemoryAllocationException
owner
- The owner to associate with the memory segment, for the fallback release.numPages
- The number of pages to allocate.MemoryAllocationException
- Thrown, if this memory manager does not have the requested amount
of memory pages any more.public void allocatePages(Object owner, List<MemorySegment> target, int numPages) throws MemoryAllocationException
owner
- The owner to associate with the memory segment, for the fallback release.target
- The list into which to put the allocated memory pages.numPages
- The number of pages to allocate.MemoryAllocationException
- Thrown, if this memory manager does not have the requested amount
of memory pages any more.public void release(MemorySegment segment)
If the memory manager manages pre-allocated memory, the memory segment goes back to the memory pool. Otherwise, the segment is only freed and made eligible for reclamation by the GC.
segment
- The segment to be released.IllegalArgumentException
- Thrown, if the given segment is of an incompatible type.public void release(Collection<MemorySegment> segments)
If the memory manager manages pre-allocated memory, the memory segment goes back to the memory pool. Otherwise, the segment is only freed and made eligible for reclamation by the GC.
segments
- The segments to be released.NullPointerException
- Thrown, if the given collection is null.IllegalArgumentException
- Thrown, id the segments are of an incompatible type.public void releaseAll(Object owner)
owner
- The owner memory segments are to be released.public MemoryType getMemoryType()
public boolean isPreAllocated()
public int getPageSize()
public long getMemorySize()
public int getTotalNumPages()
public int computeNumberOfPages(double fraction)
fraction
- the fraction of the total memory per slotpublic long computeMemorySize(double fraction)
fraction
- The fraction of the memory of the task slot.public long roundDownToPageSizeMultiple(long numBytes)
Copyright © 2014–2017 The Apache Software Foundation. All rights reserved.