Netty PooledDirect ByteBuf 和 ArrayList 意外行为
Netty PooledDirect ByteBuf and ArrayList unexpected behaviour
这似乎是一个愚蠢的错误,因为我找不到为什么没有删除所有条目。我是 运行 JAVA_OPTS:
-XX:MaxDirectMemorySize=67108864 -Dio.netty.leakDetectionLevel=advanced -D-Dio.netty.allocator.type=pooled -XX:+UseG1GC -Xms40m -Xmx40m -Dio.netty.allocator.numDirectArenas=4
完整代码如下:
private ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
//Configure the arena
//java -Dio.netty.allocator.numDirectArenas=... -Dio.netty.allocator.numHeapArenas=...
public ByteBuf createObject(int size){
return alloc.directBuffer(size);
}
public static void main(String[] args) throws InterruptedException {
ArrayList<ByteBuf> arr = new ArrayList<>();
try {
DiameterLoggingConfiguration.initLogger();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByufferTest bt = new ByufferTest();
//Total of 66060288 ~ slightly less than 64 mb of direct memory
for(int i=0;i<64512;i++){
//Each instance of 1024 bytes
arr.add(bt.createObject(1024));
}
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Now releasing.."+arr.size());
for(int i=0;i<arr.size();i++){
ByteBuf b = arr.remove(i);
b.release();
}
System.out.println("Now array size ="+arr.size());
Thread.sleep(100000);
}
输出为:
Now releasing..64512
After array size =32256
不知道只删除了恰好一半的条目。 ArrayList 中仍然有 ByteBuf 条目。
那是因为你使用了:
for(int i=0;i<arr.size();i++){
ByteBuf b = arr.remove(i);
b.release();
}
这将不起作用,因为您将索引递增 0,但同时删除了缓冲区。这样您将跳过缓冲区。
最好使用 Queue
,只使用 poll()
;
Queue<ByteBuf> arr = new ArrayDeque<>();
for(;;){
ByteBuf b = arr.poll();
if (b == null) {
break;
}
b.release();
}
这似乎是一个愚蠢的错误,因为我找不到为什么没有删除所有条目。我是 运行 JAVA_OPTS:
-XX:MaxDirectMemorySize=67108864 -Dio.netty.leakDetectionLevel=advanced -D-Dio.netty.allocator.type=pooled -XX:+UseG1GC -Xms40m -Xmx40m -Dio.netty.allocator.numDirectArenas=4
完整代码如下:
private ByteBufAllocator alloc = PooledByteBufAllocator.DEFAULT;
//Configure the arena
//java -Dio.netty.allocator.numDirectArenas=... -Dio.netty.allocator.numHeapArenas=...
public ByteBuf createObject(int size){
return alloc.directBuffer(size);
}
public static void main(String[] args) throws InterruptedException {
ArrayList<ByteBuf> arr = new ArrayList<>();
try {
DiameterLoggingConfiguration.initLogger();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByufferTest bt = new ByufferTest();
//Total of 66060288 ~ slightly less than 64 mb of direct memory
for(int i=0;i<64512;i++){
//Each instance of 1024 bytes
arr.add(bt.createObject(1024));
}
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
try {
br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Now releasing.."+arr.size());
for(int i=0;i<arr.size();i++){
ByteBuf b = arr.remove(i);
b.release();
}
System.out.println("Now array size ="+arr.size());
Thread.sleep(100000);
}
输出为:
Now releasing..64512 After array size =32256
不知道只删除了恰好一半的条目。 ArrayList 中仍然有 ByteBuf 条目。
那是因为你使用了:
for(int i=0;i<arr.size();i++){
ByteBuf b = arr.remove(i);
b.release();
}
这将不起作用,因为您将索引递增 0,但同时删除了缓冲区。这样您将跳过缓冲区。
最好使用 Queue
,只使用 poll()
;
Queue<ByteBuf> arr = new ArrayDeque<>();
for(;;){
ByteBuf b = arr.poll();
if (b == null) {
break;
}
b.release();
}