ByteBuf is always reference counted

To control the life cycle of a ByteBuf in a more predictable way, Netty does not rely on the garbage collector anymore but employs an explicit reference counter. Here's the basic rule:

  • When a buffer is allocated, its initial reference count is 1.
  • If the reference count of the buffer is decreased to 0, it is deallocated or returned to the pool it originated from.
  • The following attempts trigger an IllegalReferenceCountException:
    • Accessing a buffer whose reference count is 0,
    • Decreasing the reference count to a negative value, or
    • Increasing the reference count beyond Integer.MAX_VALUE.
  • Derived buffers (e.g. slices and duplicates) and swapped buffers (i.e. little endian buffers) share the reference count with the buffer it was derived from. Note the the reference count does not change when a derived buffer is created.

When a ByteBuf is used in a ChannelPipeline, there are additional rules you need to keep in mind:

  • Each inbound (a.k.a. upstream) handler in a pipeline has to release the received messages. Netty does not release them automatically for you.

    • Note that codec framework does release the messages automatically and a user has to increase the reference count if he or she wants to pass a message as-is to the next handler.
  • When an outbound (a.k.a. downstream) message reaches at the beginning of the pipeline, Netty will release it after writing it out.

Automatic buffer leak detection

Although reference counting is very powerful, it is also error-prone. To help a user find where he or she forgot to release the buffers, the leak detector logs the stack trace of the location where the leaked buffer was allocated automatically.

Because the leak detector relies on PhantomReference and obtaining a stack trace is a very expensive operation, it samples approximately 1% of allocations only. Therefore, it's a good idea to run the application for a reasonably long time to find all possible leaks.

Once all leaks are found and fixed. You can turn this feature off to remove its runtime overhead completely by specifying the -Dio.netty.noResourceLeakDetection JVM option.

Netty Message RefCount的更多相关文章

  1. netty——私有协议栈开发案例

    netty--私有协议栈开发案例 摘要: 在学习李林峰老师的Netty权威指南中,觉得第十二章<私有协议栈开发>中的案例代码比较有代表性,讲的也不错,但是代码中个人认为有些简单的错误,个人 ...

  2. 基于Protostuff实现的Netty编解码器

    在设计netty的编解码器过程中,有许多组件可以选择,这里由于咱对Protostuff比较熟悉,所以就用这个组件了.由于数据要在网络上传输,所以在发送方需要将类对象转换成二进制,接收方接收到数据后,需 ...

  3. alluxio源码解析-netty部分(2)

    netty简介 Netty是 一个异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端.   netty作为alluxio中重要的通讯组件 在常见的客户端上传,下载中,都会有n ...

  4. Eclipse 4.2 failed to start after TEE is installed

    ---------------  VM Arguments---------------  jvm_args: -Dosgi.requiredJavaVersion=1.6 -Dhelp.lucene ...

  5. netty系列之:选byte还是选message?这是一个问题

    目录 简介 类型的定义 搭建UDT stream服务器 搭建UDT message服务器 Stream和Message的handler 总结 简介 UDT给了你两种选择,byte stream或者me ...

  6. netty SimpleChannelInboundHandler<Message>和ChannelInboundHandlerApter

    一个兄弟的测试体验:https://blog.csdn.net/linuu/article/details/51307060 比较官方:https://www.imooc.com/article/28 ...

  7. 从netty-example分析Netty组件续

    上文我们从netty-example的Discard服务器端示例分析了netty的组件,今天我们从另一个简单的示例Echo客户端分析一下上个示例中没有出现的netty组件. 1. 服务端的连接处理,读 ...

  8. 从netty-example分析Netty组件

    分析netty从源码开始 准备工作: 1.下载源代码:https://github.com/netty/netty.git 我下载的版本为4.1 2. eclipse导入maven工程. netty提 ...

  9. Netty实现高性能RPC服务器优化篇之消息序列化

    在本人写的前一篇文章中,谈及有关如何利用Netty开发实现,高性能RPC服务器的一些设计思路.设计原理,以及具体的实现方案(具体参见:谈谈如何使用Netty开发实现高性能的RPC服务器).在文章的最后 ...

随机推荐

  1. 028 Partitioner:数据分区器

    Partitioner:数据分区器,决定数据到下一个RDD的时候在那一个分区 HashPartitioner:根据key的hashCode值来实现 RangePartitioner: 根据key所属范 ...

  2. 应用Mongoose开发MongoDB(3)控制器(controllers)

    控制器的基本构成与如何通过路由调用 控制器中通过建立函数并导出,实现前端对数据库的查询.新建.删除与修改的需求,并使之可以在路由中调用,完成API的封装.本文着重于结构之间的关系,具体问题解决方法将在 ...

  3. chown 详解

    chown Linux/Unix 是多用户多任务操作系统,所有的文件皆有其拥有者(Owner).利用 chown命令 可以将文件的拥有者加以改变.一般来说,这个命令只能由系统管理者(root)使用,一 ...

  4. linux timer operate

    1.gettimeofday()    ---->   http://www.linuxidc.com/Linux/2012-06/61903.htm   (一般)

  5. SSH Secure Shell Client安装和使用

    SSH是一个用来替代TELNET.FTP以及R命令的工具包,主要是想解决口令在网上明文传输的问题.为了系统安全和用户自身的 权益,推广SSH是必要的. SSH是英文Secure Shell的简写形式. ...

  6. 查询物理表字段(mysql)

    SELECT t.COLUMN_NAME AS NAME, ( CASE WHEN t.IS_NULLABLE = 'YES' THEN '1' ELSE '0' END ) AS ISNULL, ( ...

  7. 一个轻巧高效的多线程c++stream风格异步日志(一)

    一个轻巧高效的多线程c++stream风格异步日志 一个轻巧高效的多线程c++stream风格异步日志 前言 功能需求 性能需求 Logger实现 LogStream类 Logger类 LogStre ...

  8. View 事件分发

    View 事件分发 学习自 <Android开发艺术探索> 官方文档-MotionEvent 事件分发机制漫谈 View的事件分发机制,使我们了解View的工作原理继而学习如何自定义Vie ...

  9. android 地图

    ========= Y:\AMap_Android_API_3DMap_Demo\android_studio\AMap3DDemo>keytool -v -list -keystore C:\ ...

  10. BZOJ.1031.[JSOI2007]字符加密(后缀数组)

    题目链接 环可以拆成链:对字符串排序能想到后缀数组. 完了.输出时忽略长度不足n的串,输出s[sa[i]+n-1],即排名为i的字符串的末尾. //4140kb 744ms #include < ...