以下测试从PooledByteBufAllocator竞技场输出数字,当我运行它时,数字与实际分配/解除分配不匹配。 实际分配:50000 实际解除分配:50000 活动缓冲区:0
输出显示以下内容: directActive 1 directAlloc 1 directDealloc 0 heapActive 0 heapAlloc 0 heapDealloc 0
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PoolArenaMetric;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.util.ResourceLeakDetector;
import io.netty.util.ResourceLeakDetector.Level;
import org.junit.Test;
public class NettyTest2 {
public static PooledByteBufAllocator alloc = new PooledByteBufAllocator(true);
@Test
public void test() throws InterruptedException {
ResourceLeakDetector.setLevel(Level.PARANOID);
for (int i = 0; i < 50000; i++) {
ByteBuf b = alloc.buffer(1000, 1000);
b.release();
}
Thread.sleep(100);
//alloc.freeThreadLocalCache();
showActiveDirectBuffers();
showActiveHeapBuffers();
}
void showActiveDirectBuffers() {
int directActive = 0, directAlloc = 0, directDealloc = 0;
for (PoolArenaMetric arena : alloc.directArenas()) {
directActive += arena.numActiveAllocations();
directAlloc += arena.numAllocations();
directDealloc += arena.numDeallocations();
}
System.out.println("directActive " + directActive + " directAlloc " + directAlloc + " directDealloc " + directDealloc);
}
void showActiveHeapBuffers() {
int heapActive = 0, heapAlloc = 0, heapDealloc = 0;
for (PoolArenaMetric arena : alloc.heapArenas()) {
heapActive += arena.numActiveAllocations();
heapAlloc += arena.numAllocations();
heapDealloc += arena.numDeallocations();
}
System.out.println("heapActive " + heapActive + " heapAlloc " + heapAlloc + " heapDealloc " + heapDealloc);
}
}
答案 0 :(得分:0)
问题与Allocators的内部缓存有关。 具有禁用缓存的分配器显示与实际调用匹配的alloc / dealloc编号。
使用以下方式创建带有禁用缓存的PooledBufferAllocator(如https://github.com/netty/netty/issues/5275中所述)
PooledByteBufAllocator alloc = new PooledByteBufAllocator(true, 1, 1, 8192, 11, 0, 0, 0);