Java Code Examples for org.apache.flink.core.memory.MemorySegment#isFreed()

The following examples show how to use org.apache.flink.core.memory.MemorySegment#isFreed() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemoryManagerLazyAllocationTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsValid(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 2
Source File: MemoryManagerLazyAllocationTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsFreed(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (!seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 3
Source File: MemoryManagerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsValid(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 4
Source File: MemoryManagerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsFreed(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (!seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 5
Source File: MemoryManagerLazyAllocationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsValid(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 6
Source File: MemoryManagerLazyAllocationTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsFreed(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (!seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 7
Source File: MemoryManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsValid(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 8
Source File: MemoryManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsFreed(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (!seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 9
Source File: MemoryManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsValid(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 10
Source File: MemoryManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private boolean allMemorySegmentsFreed(List<MemorySegment> memSegs) {
	for (MemorySegment seg : memSegs) {
		if (!seg.isFreed()) {
			return false;
		}
	}
	return true;
}
 
Example 11
Source File: MemoryManager.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to release the memory for the specified segment. If the segment has already been released or
 * is null, the request is simply ignored.
 *
 * <p>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.
 *
 * @param segment The segment to be released.
 * @throws IllegalArgumentException Thrown, if the given segment is of an incompatible type.
 */
public void release(MemorySegment segment) {
	// check if segment is null or has already been freed
	if (segment == null || segment.getOwner() == null) {
		return;
	}

	final Object owner = segment.getOwner();

	// -------------------- BEGIN CRITICAL SECTION -------------------
	synchronized (lock) {
		// prevent double return to this memory manager
		if (segment.isFreed()) {
			return;
		}
		if (isShutDown) {
			throw new IllegalStateException("Memory manager has been shut down.");
		}

		// remove the reference in the map for the owner
		try {
			Set<MemorySegment> segsForOwner = this.allocatedSegments.get(owner);

			if (segsForOwner != null) {
				segsForOwner.remove(segment);
				if (segsForOwner.isEmpty()) {
					this.allocatedSegments.remove(owner);
				}
			}

			if (isPreAllocated) {
				// release the memory in any case
				memoryPool.returnSegmentToPool(segment);
			}
			else {
				segment.free();
				numNonAllocatedPages++;
			}
		}
		catch (Throwable t) {
			throw new RuntimeException("Error removing book-keeping reference to allocated memory segment.", t);
		}
	}
	// -------------------- END CRITICAL SECTION -------------------
}
 
Example 12
Source File: MemoryManager.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to release many memory segments together.
 *
 * <p>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.
 *
 * @param segments The segments to be released.
 * @throws NullPointerException Thrown, if the given collection is null.
 * @throws IllegalArgumentException Thrown, id the segments are of an incompatible type.
 */
public void release(Collection<MemorySegment> segments) {
	if (segments == null) {
		return;
	}

	// -------------------- BEGIN CRITICAL SECTION -------------------
	synchronized (lock) {
		if (isShutDown) {
			throw new IllegalStateException("Memory manager has been shut down.");
		}

		// since concurrent modifications to the collection
		// can disturb the release, we need to try potentially multiple times
		boolean successfullyReleased = false;
		do {
			final Iterator<MemorySegment> segmentsIterator = segments.iterator();

			Object lastOwner = null;
			Set<MemorySegment> segsForOwner = null;

			try {
				// go over all segments
				while (segmentsIterator.hasNext()) {

					final MemorySegment seg = segmentsIterator.next();
					if (seg == null || seg.isFreed()) {
						continue;
					}

					final Object owner = seg.getOwner();

					try {
						// get the list of segments by this owner only if it is a different owner than for
						// the previous one (or it is the first segment)
						if (lastOwner != owner) {
							lastOwner = owner;
							segsForOwner = this.allocatedSegments.get(owner);
						}

						// remove the segment from the list
						if (segsForOwner != null) {
							segsForOwner.remove(seg);
							if (segsForOwner.isEmpty()) {
								this.allocatedSegments.remove(owner);
							}
						}

						if (isPreAllocated) {
							memoryPool.returnSegmentToPool(seg);
						}
						else {
							seg.free();
							numNonAllocatedPages++;
						}
					}
					catch (Throwable t) {
						throw new RuntimeException(
								"Error removing book-keeping reference to allocated memory segment.", t);
					}
				}

				segments.clear();

				// the only way to exit the loop
				successfullyReleased = true;
			}
			catch (ConcurrentModificationException | NoSuchElementException e) {
				// this may happen in the case where an asynchronous
				// call releases the memory. fall through the loop and try again
			}
		} while (!successfullyReleased);
	}
	// -------------------- END CRITICAL SECTION -------------------
}
 
Example 13
Source File: MemoryManager.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to release the memory for the specified segment. If the segment has already been released or
 * is null, the request is simply ignored.
 *
 * <p>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.
 *
 * @param segment The segment to be released.
 * @throws IllegalArgumentException Thrown, if the given segment is of an incompatible type.
 */
public void release(MemorySegment segment) {
	// check if segment is null or has already been freed
	if (segment == null || segment.getOwner() == null) {
		return;
	}

	final Object owner = segment.getOwner();

	// -------------------- BEGIN CRITICAL SECTION -------------------
	synchronized (lock) {
		// prevent double return to this memory manager
		if (segment.isFreed()) {
			return;
		}
		if (isShutDown) {
			throw new IllegalStateException("Memory manager has been shut down.");
		}

		// remove the reference in the map for the owner
		try {
			Set<MemorySegment> segsForOwner = this.allocatedSegments.get(owner);

			if (segsForOwner != null) {
				segsForOwner.remove(segment);
				if (segsForOwner.isEmpty()) {
					this.allocatedSegments.remove(owner);
				}
			}

			if (isPreAllocated) {
				// release the memory in any case
				memoryPool.returnSegmentToPool(segment);
			}
			else {
				segment.free();
				numNonAllocatedPages++;
			}
		}
		catch (Throwable t) {
			throw new RuntimeException("Error removing book-keeping reference to allocated memory segment.", t);
		}
	}
	// -------------------- END CRITICAL SECTION -------------------
}
 
Example 14
Source File: MemoryManager.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tries to release many memory segments together.
 *
 * <p>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.
 *
 * @param segments The segments to be released.
 * @throws NullPointerException Thrown, if the given collection is null.
 * @throws IllegalArgumentException Thrown, id the segments are of an incompatible type.
 */
public void release(Collection<MemorySegment> segments) {
	if (segments == null) {
		return;
	}

	// -------------------- BEGIN CRITICAL SECTION -------------------
	synchronized (lock) {
		if (isShutDown) {
			throw new IllegalStateException("Memory manager has been shut down.");
		}

		// since concurrent modifications to the collection
		// can disturb the release, we need to try potentially multiple times
		boolean successfullyReleased = false;
		do {
			final Iterator<MemorySegment> segmentsIterator = segments.iterator();

			Object lastOwner = null;
			Set<MemorySegment> segsForOwner = null;

			try {
				// go over all segments
				while (segmentsIterator.hasNext()) {

					final MemorySegment seg = segmentsIterator.next();
					if (seg == null || seg.isFreed()) {
						continue;
					}

					final Object owner = seg.getOwner();

					try {
						// get the list of segments by this owner only if it is a different owner than for
						// the previous one (or it is the first segment)
						if (lastOwner != owner) {
							lastOwner = owner;
							segsForOwner = this.allocatedSegments.get(owner);
						}

						// remove the segment from the list
						if (segsForOwner != null) {
							segsForOwner.remove(seg);
							if (segsForOwner.isEmpty()) {
								this.allocatedSegments.remove(owner);
							}
						}

						if (isPreAllocated) {
							memoryPool.returnSegmentToPool(seg);
						}
						else {
							seg.free();
							numNonAllocatedPages++;
						}
					}
					catch (Throwable t) {
						throw new RuntimeException(
								"Error removing book-keeping reference to allocated memory segment.", t);
					}
				}

				segments.clear();

				// the only way to exit the loop
				successfullyReleased = true;
			}
			catch (ConcurrentModificationException | NoSuchElementException e) {
				// this may happen in the case where an asynchronous
				// call releases the memory. fall through the loop and try again
			}
		} while (!successfullyReleased);
	}
	// -------------------- END CRITICAL SECTION -------------------
}