0

这似乎是一个愚蠢的错误,因为我找不到为什么所有条目都没有被删除。我正在使用 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);

}

输出是:

现在发布..64512 后数组大小=32256

不知道只有一半的条目被删除。ArrayList 中仍然有 ByteBuf 条目。

4

1 回答 1

1

那是因为你使用:

 for(int i=0;i<arr.size();i++){
    ByteBuf b = arr.remove(i);
    b.release();
}

这将不起作用,因为您将索引增加 0 但也会删除缓冲区。这样,您将跳过缓冲区。

更好地使用 anQueue并且只使用poll();

 Queue<ByteBuf> arr = new ArrayDeque<>();
 for(;;){
    ByteBuf b = arr.poll();
    if (b == null) {
        break;
    }
    b.release();
}
于 2017-02-02T09:41:36.467 回答