public class MemoryManager extends Object
MemorySegments of equal size and arbitrary type or in reserved chunks of certain size and MemoryType.
Operators allocate the memory either by requesting a number of memory segments or by reserving chunks.
Any allocated memory has to be released to be reused later.
Which MemoryTypes the MemoryManager serves and their total sizes can be passed as an argument
to the constructor.
The memory segments may be represented as on-heap byte arrays or as off-heap memory regions
(both via HybridMemorySegment). Releasing a memory segment will make it re-claimable
by the garbage collector.
| Modifier and Type | Class and Description |
|---|---|
static class |
MemoryManager.AllocationRequest
Memory segment allocation request.
|
static class |
MemoryManager.Builder
A builder for the
MemoryManager.AllocationRequest. |
| 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(Map<org.apache.flink.core.memory.MemoryType,Long> memorySizeByType,
int pageSize)
Creates a memory manager with the given memory types, capacity and given page size.
|
| Modifier and Type | Method and Description |
|---|---|
Collection<org.apache.flink.core.memory.MemorySegment> |
allocatePages(MemoryManager.AllocationRequest request)
Allocates a set of memory segments from this memory manager.
|
void |
allocatePages(Object owner,
Collection<org.apache.flink.core.memory.MemorySegment> target,
int numberOfPages)
Deprecated.
|
List<org.apache.flink.core.memory.MemorySegment> |
allocatePages(Object owner,
int numPages)
Deprecated.
|
long |
availableMemory(org.apache.flink.core.memory.MemoryType memoryType)
Returns the total size of the certain type of memory handled by this memory manager.
|
long |
computeMemorySize(double fraction)
Computes the memory size corresponding to the fraction of all memory governed by this MemoryManager.
|
int |
computeNumberOfPages(double fraction)
Computes to how many pages the given number of bytes corresponds.
|
static MemoryManager |
forDefaultPageSize(long size) |
<T extends AutoCloseable> |
getExternalSharedMemoryResource(String type,
org.apache.flink.util.function.LongFunctionWithException<T,Exception> initializer,
long numBytes)
Acquires a shared resource, identified by a type string.
|
long |
getMemorySize()
Returns the total size of memory handled by this memory manager.
|
long |
getMemorySizeByType(org.apache.flink.core.memory.MemoryType memoryType)
Returns the total size of the certain type of memory handled by this memory manager.
|
int |
getPageSize()
Gets the size of the pages handled by the memory manager.
|
<T extends AutoCloseable> |
getSharedMemoryResourceForManagedMemory(String type,
org.apache.flink.util.function.LongFunctionWithException<T,Exception> initializer)
Acquires a shared memory resource, that uses all the memory of this memory manager.
|
<T extends AutoCloseable> |
getSharedMemoryResourceForManagedMemory(String type,
org.apache.flink.util.function.LongFunctionWithException<T,Exception> initializer,
double fractionToInitializeWith)
Acquires a shared memory resource, identified by a type string.
|
boolean |
isShutdown()
Checks whether the MemoryManager has been shut down.
|
void |
release(Collection<org.apache.flink.core.memory.MemorySegment> segments)
Tries to release many memory segments together.
|
void |
release(org.apache.flink.core.memory.MemorySegment segment)
Tries to release the memory for the specified segment.
|
void |
releaseAll(Object owner)
Releases all memory segments for the given owner.
|
void |
releaseAllMemory(Object owner,
org.apache.flink.core.memory.MemoryType memoryType)
Releases all memory of a certain type from an owner to this memory manager.
|
void |
releaseMemory(Object owner,
org.apache.flink.core.memory.MemoryType memoryType,
long size)
Releases memory of a certain type from an owner to this memory manager.
|
void |
reserveMemory(Object owner,
org.apache.flink.core.memory.MemoryType memoryType,
long size)
Reserves memory of a certain type for an owner from this memory manager.
|
void |
shutdown()
Shuts the memory manager down, trying to release all the memory it managed.
|
boolean |
verifyEmpty()
Checks if the memory manager's memory is completely available (nothing allocated at the moment).
|
public static final int DEFAULT_PAGE_SIZE
public static final int MIN_PAGE_SIZE
public MemoryManager(Map<org.apache.flink.core.memory.MemoryType,Long> memorySizeByType, int pageSize)
memorySizeByType - The total size of the memory to be managed by this memory manager for each type (heap / off-heap).pageSize - The size of the pages handed out by the memory manager.public void shutdown()
@VisibleForTesting public boolean isShutdown()
public boolean verifyEmpty()
@Deprecated public List<org.apache.flink.core.memory.MemorySegment> allocatePages(Object owner, int numPages) throws MemoryAllocationException
allocatePages(AllocationRequest)The returned segments can have any memory type. The total allocated memory for each type will not exceed its size limit, announced in the constructor.
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.@Deprecated public void allocatePages(Object owner, Collection<org.apache.flink.core.memory.MemorySegment> target, int numberOfPages) throws MemoryAllocationException
allocatePages(AllocationRequest)The allocated segments can have any memory type. The total allocated memory for each type will not exceed its size limit, announced in the constructor.
owner - The owner to associate with the memory segment, for the fallback release.target - The list into which to put the allocated memory pages.numberOfPages - The number of pages to allocate.MemoryAllocationException - Thrown, if this memory manager does not have the requested amount
of memory pages any more.public Collection<org.apache.flink.core.memory.MemorySegment> allocatePages(MemoryManager.AllocationRequest request) throws MemoryAllocationException
The allocated segments can have any memory type. The total allocated memory for each type will not exceed its size limit, announced in the constructor.
request - The allocation request which contains all the parameters.MemoryAllocationException - Thrown, if this memory manager does not have the requested amount
of memory pages any more.public void release(org.apache.flink.core.memory.MemorySegment segment)
If the segment has already been released, it is only freed. If it is null or has no owner, the request is simply ignored. The segment is only freed and made eligible for reclamation by the GC. The segment will be returned to the memory pool of its type, increasing its available limit for the later allocations.
segment - The segment to be released.IllegalArgumentException - Thrown, if the given segment is of an incompatible type.public void release(Collection<org.apache.flink.core.memory.MemorySegment> segments)
The segment is only freed and made eligible for reclamation by the GC. Each segment will be returned to the memory pool of its type, increasing its available limit for the later allocations.
segments - The segments to be released.IllegalArgumentException - Thrown, if the segments are of an incompatible type.public void releaseAll(Object owner)
owner - The owner memory segments are to be released.public void reserveMemory(Object owner, org.apache.flink.core.memory.MemoryType memoryType, long size) throws MemoryReservationException
owner - The owner to associate with the memory reservation, for the fallback release.memoryType - type of memory to reserve (heap / off-heap).size - size of memory to reserve.MemoryReservationException - Thrown, if this memory manager does not have the requested amount
of memory any more.public void releaseMemory(Object owner, org.apache.flink.core.memory.MemoryType memoryType, long size)
owner - The owner to associate with the memory reservation, for the fallback release.memoryType - type of memory to release (heap / off-heap).size - size of memory to release.public void releaseAllMemory(Object owner, org.apache.flink.core.memory.MemoryType memoryType)
owner - The owner to associate with the memory reservation, for the fallback release.memoryType - type of memory to release (heap / off-heap).public <T extends AutoCloseable> OpaqueMemoryResource<T> getSharedMemoryResourceForManagedMemory(String type, org.apache.flink.util.function.LongFunctionWithException<T,Exception> initializer) throws Exception
getSharedMemoryResourceForManagedMemory(String, LongFunctionWithException, double).Exceptionpublic <T extends AutoCloseable> OpaqueMemoryResource<T> getSharedMemoryResourceForManagedMemory(String type, org.apache.flink.util.function.LongFunctionWithException<T,Exception> initializer, double fractionToInitializeWith) throws Exception
The memory for the resource is reserved from the memory budget of this memory manager (thus determining the size of the resource), but resource itself is opaque, meaning the memory manager does not understand its structure.
The OpaqueMemoryResource object returned from this method must be closed once not used any further. Once all acquisitions have closed the object, the resource itself is closed.
Important: The failure semantics are as follows: If the memory manager fails to reserve
the memory, the external resource initializer will not be called. If an exception is thrown when the
opaque resource is closed (last lease is released), the memory manager will still un-reserve the
memory to make sure its own accounting is clean. The exception will need to be handled by the caller of
OpaqueMemoryResource.close(). For example, if this indicates that native memory was not released
and the process might thus have a memory leak, the caller can decide to kill the process as a result.
Exceptionpublic <T extends AutoCloseable> OpaqueMemoryResource<T> getExternalSharedMemoryResource(String type, org.apache.flink.util.function.LongFunctionWithException<T,Exception> initializer, long numBytes) throws Exception
The resource opaque, meaning the memory manager does not understand its structure.
The OpaqueMemoryResource object returned from this method must be closed once not used any further. Once all acquisitions have closed the object, the resource itself is closed.
Exceptionpublic int getPageSize()
public long getMemorySize()
public long getMemorySizeByType(org.apache.flink.core.memory.MemoryType memoryType)
memoryType - The type of memory.public long availableMemory(org.apache.flink.core.memory.MemoryType memoryType)
memoryType - The type of memory.public int computeNumberOfPages(double fraction)
fraction - the fraction of the total memory per slotpublic long computeMemorySize(double fraction)
fraction - The fraction of all memory governed by this MemoryManagerpublic static MemoryManager forDefaultPageSize(long size)
Copyright © 2014–2020 The Apache Software Foundation. All rights reserved.