Reputation: 9
Had a question about buffer management for Netty downstream buffer sending flow.
I have a Netty SimpleChannelInBoundHandler MainHandler that does 2 things:-
As part 1, I believe I need to explicitly retain the buffer for sending it downstream to destination channel D. Do I also need to take care of releasing this buffer after writeAndFlush() for this channel? Or would Netty pipeline take care of releasing it?
Based on Netty docs https://netty.io/wiki/new-and-noteworthy-in-4.0.html, it says When an outbound (a.k.a. downstream) message reaches at the beginning of the pipeline, Netty will release it after writing it out.
which makes me think that Netty would take care of releasing the explicitly retained buffer after downstream/destination channel pipeline completes. Is this understanding correct?
`
Explicitly releasing the buffer throws ReferenceCountUtilException in the downstream pipeline
Upvotes: 0
Views: 32
Reputation: 37
Every time WriteAndFlush
is called, the reference count refCnt
of bytebuf will be set to 0, thereby releasing it.
However, when there are multiple client connections, it is not ensured that byteBuff is not released before all messages are sent, and it is necessary to manually add byteBuff .retain()
to increase the reference count by 1.
You can add .release()
in the try-finally
int count = byteBuf.refCnt();
if(count0!=0){
byteBuf.release();
}
Upvotes: 0