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

The following examples show how to use org.apache.flink.core.memory.MemorySegment#free() . 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: MemoryManager.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Shuts the memory manager down, trying to release all the memory it managed. Depending
 * on implementation details, the memory does not necessarily become reclaimable by the
 * garbage collector, because there might still be references to allocated segments in the
 * code that allocated them from the memory manager.
 */
public void shutdown() {
	// -------------------- BEGIN CRITICAL SECTION -------------------
	synchronized (lock) {
		if (!isShutDown) {
			// mark as shutdown and release memory
			isShutDown = true;
			numNonAllocatedPages = 0;

			// go over all allocated segments and release them
			for (Set<MemorySegment> segments : allocatedSegments.values()) {
				for (MemorySegment seg : segments) {
					seg.free();
				}
			}

			memoryPool.clear();
		}
	}
	// -------------------- END CRITICAL SECTION -------------------
}
 
Example 2
Source File: MemoryManager.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Releases all memory segments for the given owner.
 *
 * @param owner The owner memory segments are to be released.
 */
public void releaseAll(Object owner) {
	if (owner == null) {
		return;
	}

	Preconditions.checkState(!isShutDown, "Memory manager has been shut down.");

	// get all segments
	Set<MemorySegment> segments = allocatedSegments.remove(owner);

	// all segments may have been freed previously individually
	if (segments == null || segments.isEmpty()) {
		return;
	}

	// free each segment
	for (MemorySegment segment: segments) {
		segment.free();
	}

	segments.clear();
}
 
Example 3
Source File: MemoryManager.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Shuts the memory manager down, trying to release all the memory it managed. Depending
 * on implementation details, the memory does not necessarily become reclaimable by the
 * garbage collector, because there might still be references to allocated segments in the
 * code that allocated them from the memory manager.
 */
public void shutdown() {
	// -------------------- BEGIN CRITICAL SECTION -------------------
	synchronized (lock) {
		if (!isShutDown) {
			// mark as shutdown and release memory
			isShutDown = true;
			numNonAllocatedPages = 0;

			// go over all allocated segments and release them
			for (Set<MemorySegment> segments : allocatedSegments.values()) {
				for (MemorySegment seg : segments) {
					seg.free();
				}
			}

			memoryPool.clear();
		}
	}
	// -------------------- END CRITICAL SECTION -------------------
}
 
Example 4
Source File: MemoryManager.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Shuts the memory manager down, trying to release all the memory it managed. Depending
 * on implementation details, the memory does not necessarily become reclaimable by the
 * garbage collector, because there might still be references to allocated segments in the
 * code that allocated them from the memory manager.
 */
public void shutdown() {
	if (!isShutDown) {
		// mark as shutdown and release memory
		isShutDown = true;
		reservedMemory.clear();

		// go over all allocated segments and release them
		for (Set<MemorySegment> segments : allocatedSegments.values()) {
			for (MemorySegment seg : segments) {
				seg.free();
			}
			segments.clear();
		}
		allocatedSegments.clear();
	}
}
 
Example 5
Source File: NetworkBufferPool.java    From flink with Apache License 2.0 5 votes vote down vote up
public void destroy() {
	synchronized (factoryLock) {
		isDestroyed = true;
	}

	synchronized (availableMemorySegments) {
		MemorySegment segment;
		while ((segment = availableMemorySegments.poll()) != null) {
			segment.free();
		}
	}
}
 
Example 6
Source File: SpilledSubpartitionView.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void recycle(MemorySegment memorySegment) {
	synchronized (buffers) {
		if (isDestroyed) {
			memorySegment.free();
		} else {
			buffers.add(new NetworkBuffer(memorySegment, this));
			buffers.notifyAll();
		}
	}
}
 
Example 7
Source File: NetworkBufferPool.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public void destroy() {
	synchronized (factoryLock) {
		isDestroyed = true;

		MemorySegment segment;
		while ((segment = availableMemorySegments.poll()) != null) {
			segment.free();
		}
	}
}
 
Example 8
Source File: NetworkBufferPoolTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link NetworkBufferPool#requestMemorySegments(int)}, verifying it may be aborted in
 * case of a concurrent {@link NetworkBufferPool#destroy()} call.
 */
@Test
public void testRequestMemorySegmentsInterruptable() throws Exception {
	final int numBuffers = 10;

	NetworkBufferPool globalPool = new NetworkBufferPool(numBuffers, 128);
	MemorySegment segment = globalPool.requestMemorySegment();
	assertNotNull(segment);

	final OneShotLatch isRunning = new OneShotLatch();
	CheckedThread asyncRequest = new CheckedThread() {
		@Override
		public void go() throws Exception {
			isRunning.trigger();
			globalPool.requestMemorySegments(10);
		}
	};
	asyncRequest.start();

	// We want the destroy call inside the blocking part of the globalPool.requestMemorySegments()
	// call above. We cannot guarantee this though but make it highly probable:
	isRunning.await();
	Thread.sleep(10);
	globalPool.destroy();

	segment.free();

	expectedException.expect(IllegalStateException.class);
	expectedException.expectMessage("destroyed");
	try {
		asyncRequest.sync();
	} finally {
		globalPool.destroy();
	}
}
 
Example 9
Source File: NetworkBufferPool.java    From flink with Apache License 2.0 5 votes vote down vote up
public void destroy() {
	synchronized (factoryLock) {
		isDestroyed = true;

		MemorySegment segment;
		while ((segment = availableMemorySegments.poll()) != null) {
			segment.free();
		}
	}
}
 
Example 10
Source File: NetworkBufferPoolTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link NetworkBufferPool#requestMemorySegments()}, verifying it may be aborted in
 * case of a concurrent {@link NetworkBufferPool#destroy()} call.
 */
@Test
public void testRequestMemorySegmentsInterruptable() throws Exception {
	final int numBuffers = 10;

	NetworkBufferPool globalPool = new NetworkBufferPool(numBuffers, 128, 10);
	MemorySegment segment = globalPool.requestMemorySegment();
	assertNotNull(segment);

	final OneShotLatch isRunning = new OneShotLatch();
	CheckedThread asyncRequest = new CheckedThread() {
		@Override
		public void go() throws Exception {
			isRunning.trigger();
			globalPool.requestMemorySegments();
		}
	};
	asyncRequest.start();

	// We want the destroy call inside the blocking part of the globalPool.requestMemorySegments()
	// call above. We cannot guarantee this though but make it highly probable:
	isRunning.await();
	Thread.sleep(10);
	globalPool.destroy();

	segment.free();

	expectedException.expect(IllegalStateException.class);
	expectedException.expectMessage("destroyed");
	try {
		asyncRequest.sync();
	} finally {
		globalPool.destroy();
	}
}
 
Example 11
Source File: NetworkBufferPoolTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests {@link NetworkBufferPool#requestMemorySegments()}, verifying it may be aborted in
 * case of a concurrent {@link NetworkBufferPool#destroy()} call.
 */
@Test
public void testRequestMemorySegmentsInterruptable() throws Exception {
	final int numBuffers = 10;

	NetworkBufferPool globalPool = new NetworkBufferPool(numBuffers, 128, 10);
	MemorySegment segment = globalPool.requestMemorySegment();
	assertNotNull(segment);

	final OneShotLatch isRunning = new OneShotLatch();
	CheckedThread asyncRequest = new CheckedThread() {
		@Override
		public void go() throws Exception {
			isRunning.trigger();
			globalPool.requestMemorySegments();
		}
	};
	asyncRequest.start();

	// We want the destroy call inside the blocking part of the globalPool.requestMemorySegments()
	// call above. We cannot guarantee this though but make it highly probable:
	isRunning.await();
	Thread.sleep(10);
	globalPool.destroy();

	segment.free();

	expectedException.expect(IllegalStateException.class);
	expectedException.expectMessage("destroyed");
	try {
		asyncRequest.sync();
	} finally {
		globalPool.destroy();
	}
}
 
Example 12
Source File: MemorySegmentPool.java    From stateful-functions with Apache License 2.0 5 votes vote down vote up
void release(MemorySegment segment) {
  if (totalAllocatedMemory > inMemoryBufferSize) {
    //
    // we previously overdraft.
    //
    segment.free();
    totalAllocatedMemory -= PAGE_SIZE;
    return;
  }
  pool.add(segment);
}
 
Example 13
Source File: MemorySegmentPool.java    From flink-statefun with Apache License 2.0 5 votes vote down vote up
void release(MemorySegment segment) {
  if (totalAllocatedMemory > inMemoryBufferSize) {
    //
    // we previously overdraft.
    //
    segment.free();
    totalAllocatedMemory -= PAGE_SIZE;
    return;
  }
  pool.add(segment);
}
 
Example 14
Source File: FreeingBufferRecycler.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Frees the given memory segment.
 * @param memorySegment The memory segment to be recycled.
 */
@Override
public void recycle(MemorySegment memorySegment) {
	memorySegment.free();
}
 
Example 15
Source File: MemoryManager.java    From flink with Apache License 2.0 4 votes vote down vote up
private static void freeSegment(MemorySegment segment, @Nullable Collection<MemorySegment> segments) {
	segment.free();
	if (segments != null) {
		segments.remove(segment);
	}
}
 
Example 16
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 -------------------
}
 
Example 17
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 18
Source File: FreeingBufferRecycler.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Frees the given memory segment.
 * @param memorySegment The memory segment to be recycled.
 */
@Override
public void recycle(MemorySegment memorySegment) {
	memorySegment.free();
}
 
Example 19
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 20
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 -------------------
}