netty源码阅读之UnpooledByteBufAllocator

使用IDEA阅读源码Navigate下面的工具是个好东西 。可以帮助分析类的结构等

ByteBufAllocator主要用来生成三种ByteBuf :HeadBuffer,DirectBuffer,CompositeBuffer. 还有一个ByteBufAllocator DEFAULT 静态属性。 我们可以通过io.netty.allocator.type来控制该静态属性的类型:unpooled和pooled (是否使用缓冲池)。

 

用于ByteBuf是通过引用计数来管理内存。在AbstractByteBufAllocator引用了一个toLeakAwareBuffer方法帮助分析内存是否泄漏 。具体参考http://blog.csdn.net/damacheng/article/details/42393757

netty根据是否使用缓存池提供了两个实现类。UnpooledByteBufAllocator和PooledByteBufAllocator(这个比较复杂,实现了jemalloc的算法)

 

UnpooledByteBufAllocator根据使用使用sun.misc.Unsafe类提供了两种直接缓存

UnpooledUnsafeDirectByteBuf和UnpooledDirectByteBuf  一个使用unsafe来对直接缓冲区进行操作,一个使用bytebuffer的方法对缓冲区进行操作。

后面看下两种的不同。以便加深对unsafe的认识
posted @ 2015-02-27 20:32  高兴的博客  阅读(7471)  评论(0编辑  收藏  举报