Buffer ownership in Netty 4: How is buffer life-cycle managed?

前端 未结 1 2047
星月不相逢
星月不相逢 2020-12-31 12:46

I\'ve been trying to write an HTTP client to fetch multiple feeds concurrently (up to 1k), also as an exercise to learn Netty 4.

My question is, if there is a good e

1条回答
  •  隐瞒了意图╮
    2020-12-31 13:07

    HttpContent extends ReferenceCounted to keep track of the life cycle of the buffer it holds. When a ReferenceCounted is instantiated, it starts its life with refCnt of 1. If you call retain() on it, refCnt is increased. refCnt is decreased on release(), and the underlying resource (ByteBuf in this case) is destroyed once refCnt becomes 0.

    Usually, a handler does not need to keep a reference to the message it finished handling, because the message is usually thrown away or transformed into something else once handled. Therefore, release() method must be called on the message once your handler is done with it. This is often error-prone and will lead to resource leak very easily.

    To avoid the leak that is very hard to track down, extend SimpleChannelInboundHandler which calls release() automatically on the messages once they are handled.

    For more information about reference counting in Netty, please read this wiki page. It also gives you the detailed information about how to troubleshooting the buffer leaks by making use of Netty's buffer leak detection mechanism.

    0 讨论(0)
提交回复
热议问题