创建一个Netty ByteBuf

创建一个Netty ByteBuf,netty,Netty,如果我只想在Netty应用程序中使用ByteBuf,而不是在Netty应用程序中使用。我使用ByteBuf=unmooled.wrappedBuffer(字节)来创建ByteBuf,我必须在函数末尾调用buf.release public void process(byte[] bytes) { ByteBuf frame = Unpooled.wrappedBuffer(bytes); //something frame.release()

如果我只想在Netty应用程序中使用ByteBuf,而不是在Netty应用程序中使用。我使用
ByteBuf=unmooled.wrappedBuffer(字节)
来创建ByteBuf,我必须在函数末尾调用
buf.release

 public void process(byte[] bytes) {

            ByteBuf frame = Unpooled.wrappedBuffer(bytes);

//something
            frame.release();
        }

unmoleedHeapByteBuf.release
方法实现如下:

@Override
public boolean release() {
    for (;;) {
        int refCnt = this.refCnt;
        if (refCnt == 0) {
            throw new IllegalReferenceCountException(0, -1);
        }

        if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {
            if (refCnt == 1) {
                deallocate();
                return true;
            }
            return false;
        }
    }
}
以及
解除分配
方法:

@Override
protected void deallocate() {
    array = null;
}

堆内存可以被垃圾收集器回收,所以在我看来,不调用
release
方法在理论上可能不会造成内存泄漏。

unsoleedheapbytebuf.release
方法实现如下:

@Override
public boolean release() {
    for (;;) {
        int refCnt = this.refCnt;
        if (refCnt == 0) {
            throw new IllegalReferenceCountException(0, -1);
        }

        if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {
            if (refCnt == 1) {
                deallocate();
                return true;
            }
            return false;
        }
    }
}
以及
解除分配
方法:

@Override
protected void deallocate() {
    array = null;
}

堆内存可以由垃圾收集器回收,因此在我看来,不调用
release
方法在理论上可能不会造成内存泄漏。

根据经验,在使用
ByteBuf
之后,您应该始终调用
ByteBuf.release()
。这将允许您稍后在不更改任何代码的情况下切换到直接缓冲区或池缓冲区。

根据经验,在使用
ByteBuf
后,您应该始终调用
ByteBuf.release()
。这将允许您稍后切换到直接缓冲区或池缓冲区,而无需任何代码更改