我正在使用 Netty 4.1.17-Final。
我编写了发送和接收 100 MB 随机 ASCII 的代码。解码器在 ByteBuf 变为 100 MB 之前不会读取。
@Override
public void decode(ByteBuffer _in, List<Object> _out) {
if (_in.remaining() == size) { // size = 100 * 1024 * 1024
_in.get(new byte[size]);
}
}
因此,Netty 缓冲了 100 MB,但即使通过监控 Direct Memory 也没有发现。
System.out.println(sun.misc.SharedSecrets.getJavaNioAccess().getDirectBufferPool().getMemoryUsed());
ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class).forEach(buf -> {
System.out.printf("%s: Used=%d, TotalCap=%d%n", buf.getName(), buf.getMemoryUsed(), buf.getTotalCapacity());
});
// Result
// 10
// direct: Used=9, TotalCap=8
// mapped: Used=0, TotalCap=0
Netty 使用了 PooledUnsafeDirectByteBuf,但是你在哪里缓冲接收到的数据呢?还是直接缓冲监控方法不对?