netty bytebuff 释放原则和防止内存泄露


Action Who should release? Who released?
1. main() creates buf buf→main()
2. main() calls a() with buf buf→a()
3. a() returns buf merely. buf→main()
4. main() calls b() with buf buf→b()
5. b() returns the copy of buf buf→b(), copy→main() b() releases buf
6. main() calls c() with copy copy→c()
7. c() swallows copy copy→c() c() releases copy


  • If a [sending] component is supposed to pass a reference-counted object to another [receiving] component, the sending component usually does not need to destroy it but defers that decision to the receiving component.
  • If a component consumes a reference-counted object and knows nothing else will access it anymore (i.e., does not pass along a reference to yet another component), the component should destroy it.


1.A(byteBuff)接受了byteBuff, 然后又传给了另外一个方法B(bytebuff),A方法内不用释放;

2.如果B(bytebuff)接受了byteBuff,没有再传递给其他方法,B负责release;


http://netty.io/wiki/reference-counted-objects.html

http://stackoverflow.com/questions/15781276/buffer-ownership-in-netty-4-how-is-buffer-life-cycle-managed



你可能感兴趣的:(netty,bytebuff,refCnt)