Merge pull request #55 from netty/alloc-close

Clarify what it means to close an allocator
This commit is contained in:
Chris Vest 2021-05-05 16:39:17 +02:00 committed by GitHub
commit 86cc19bd76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 38 deletions

View File

@ -98,8 +98,17 @@ public interface BufferAllocator extends AutoCloseable {
} }
/** /**
* Close this allocator, freeing all of its internal resources. It is not specified if the allocator can still be * Close this allocator, freeing all of its internal resources.
* used after this method has been called on it. * <p>
* Existing (currently in-use) allocated buffers will not be impacted by calling this method.
* If this is a pooling or caching allocator, then existing buffers will be immediately freed when they are closed,
* instead of being pooled or cached.
* <p>
* The allocator can still be used to allocate more buffers after calling this method.
* However, if this is a pooling or caching allocator, then the pooling and caching functionality will be
* effectively disabled after calling this method.
* <p>
* If this allocator does not perform any pooling or caching, then calling this method likely has no effect.
*/ */
@Override @Override
default void close() { default void close() {

View File

@ -19,7 +19,6 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays;
import static java.nio.ByteOrder.BIG_ENDIAN; import static java.nio.ByteOrder.BIG_ENDIAN;
import static java.nio.ByteOrder.LITTLE_ENDIAN; import static java.nio.ByteOrder.LITTLE_ENDIAN;

View File

@ -16,53 +16,33 @@
package io.netty.buffer.api; package io.netty.buffer.api;
import io.netty.buffer.api.memseg.NativeMemorySegmentManager; import io.netty.buffer.api.memseg.NativeMemorySegmentManager;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class BufferCleanerTest extends BufferTestSupport { public class BufferCleanerTest extends BufferTestSupport {
@Disabled("Too slow, for now")
@ParameterizedTest @ParameterizedTest
@MethodSource("directAllocators") @MethodSource("directAllocators")
public void bufferMustBeClosedByCleaner(Fixture fixture) throws InterruptedException { public void bufferMustBeClosedByCleaner(Fixture fixture) throws InterruptedException {
var initial = NativeMemorySegmentManager.MEM_USAGE_NATIVE.sum(); var initial = NativeMemorySegmentManager.MEM_USAGE_NATIVE.sum();
int allocationSize = 1024;
allocateAndForget(fixture, allocationSize);
long sum = 0;
for (int i = 0; i < 15; i++) {
System.gc();
System.runFinalization();
sum = NativeMemorySegmentManager.MEM_USAGE_NATIVE.sum() - initial;
if (sum < allocationSize) {
return;
}
}
assertThat(sum).isLessThan(allocationSize);
}
private static void allocateAndForget(Fixture fixture, int size) {
var allocator = fixture.createAllocator(); var allocator = fixture.createAllocator();
allocator.close(); allocator.close();
int iterations = 15;
int allocationSize = 1024;
for (int i = 0; i < iterations; i++) {
allocateAndForget(allocator, allocationSize);
System.gc();
}
System.runFinalization();
var sum = NativeMemorySegmentManager.MEM_USAGE_NATIVE.sum() - initial;
var totalAllocated = (long) allocationSize * iterations;
assertThat(sum).isLessThan(totalAllocated);
}
private static void allocateAndForget(BufferAllocator allocator, int size) {
allocator.allocate(size); allocator.allocate(size);
} }
@Disabled("Too slow, for now")
@ParameterizedTest
@MethodSource("pooledDirectAllocators")
public void buffersMustBeReusedByPoolingAllocatorEvenWhenDroppedByCleanerInsteadOfExplicitly(Fixture fixture)
throws InterruptedException {
var initial = NativeMemorySegmentManager.MEM_USAGE_NATIVE.sum();
try (var allocator = fixture.createAllocator()) {
int iterations = 15;
int allocationSize = 1024;
for (int i = 0; i < iterations; i++) {
allocateAndForget(allocator, allocationSize);
System.gc();
}
System.runFinalization();
var sum = NativeMemorySegmentManager.MEM_USAGE_NATIVE.sum() - initial;
var totalAllocated = (long) allocationSize * iterations;
assertThat(sum).isLessThan(totalAllocated);
}
}
} }