2016-05-26 5 views
0

Nettyアプリケーションで使用しないByteBufを使用したい場合は、ByteBuf buf = Unpooled.wrappedBuffer(bytes)を使用してByteBufを作成します。機能の最後にbuf.releaseを呼び出す必要がありますか?Netty ByteBufを作成する

public void process(byte[] bytes) { 

      ByteBuf frame = Unpooled.wrappedBuffer(bytes); 

//something 
      frame.release(); 
     } 

答えて

1

UnpooledHeapByteBuf.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; 
     } 
    } 
} 

そしてdeallocate方法:

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

ヒープメモリがガベージコレクタによってリサイクル、ので、多分releaseメソッドを呼び出さないことができます私の意見では理論的にメモリリークを起こしません。

1

ByteBufを使用して完了した後は、経験則として常にByteBuf.release()を呼び出す必要があります。これにより、後でコードを変更することなく直接バッファまたはプールされたバッファに切り替えることができます。

関連する問題