首页  

PooledByteBufAllocator跟踪调试     所属分类 netty 浏览量 1364
echo 例子

newDirectBuffer 和 newHeapBuffer 方法上 断点调试 ,观察线程堆栈


Channel

ByteBufAllocator alloc();

ByteBufAllocator
AbstractByteBufAllocator 

PooledByteBufAllocator
UnpooledByteBufAllocator


ByteBufUtil

String allocType = SystemPropertyUtil.get("io.netty.allocator.type", "pooled").toLowerCase(Locale.US).trim();

默认分配器 PooledByteBufAllocator



public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder) {

final ThreadLocal<PoolThreadCache> threadCache = new ThreadLocal<PoolThreadCache>() {

PoolThreadCache
final PoolArena<byte[]> heapArena;
final PoolArena<ByteBuffer> directArena;


    
    
protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) 
protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) 

读取时分配内存 PooledByteBufAllocator.newDirectBuffer


Thread [nioEventLoopGroup-4-1] (Suspended)	
	PooledByteBufAllocator.newDirectBuffer(int, int) line: 247	
	PooledByteBufAllocator(AbstractByteBufAllocator).directBuffer(int, int) line: 155	
	PooledByteBufAllocator(AbstractByteBufAllocator).directBuffer(int) line: 146	
	PooledByteBufAllocator(AbstractByteBufAllocator).ioBuffer(int) line: 107	
	AbstractNioByteChannel$NioByteUnsafe.read() line: 119	
	NioEventLoop.processSelectedKey(SelectionKey, AbstractNioChannel) line: 494	
	NioEventLoop.processSelectedKeysOptimized(SelectionKey[]) line: 461	
	NioEventLoop.processSelectedKeys() line: 378	
	NioEventLoop.run() line: 350	
	SingleThreadEventExecutor$2.run() line: 101	
	Thread.run() line: 745	

写入时分配内存 PooledByteBufAllocator.newDirectBuffer


Thread [nioEventLoopGroup-4-1] (Suspended (breakpoint at line 233 in PooledByteBufAllocator))	
	PooledByteBufAllocator.newDirectBuffer(int, int) line: 233	
	PooledByteBufAllocator(AbstractByteBufAllocator).directBuffer(int, int) line: 155	
	PooledByteBufAllocator(AbstractByteBufAllocator).directBuffer(int) line: 146	
	PooledByteBufAllocator(AbstractByteBufAllocator).buffer(int) line: 83	
	ByteBufUtil.encodeString(ByteBufAllocator, CharBuffer, Charset) line: 338	
	StringEncoder.encode(ChannelHandlerContext, CharSequence, List<Object>) line: 80	
	StringEncoder.encode(ChannelHandlerContext, Object, List) line: 51	
	StringEncoder(MessageToMessageEncoder<I>).write(ChannelHandlerContext, Object, ChannelPromise) line: 89	
	DefaultChannelHandlerContext.invokeWrite(Object, ChannelPromise) line: 647	
	DefaultChannelHandlerContext.write(Object, boolean, ChannelPromise) line: 701	
	DefaultChannelHandlerContext.writeAndFlush(Object, ChannelPromise) line: 691	
	DefaultChannelHandlerContext.writeAndFlush(Object) line: 726	
	EchoServerHandler.channelRead(ChannelHandlerContext, Object) line: 32	
	DefaultChannelHandlerContext.invokeChannelRead(Object) line: 340	
	DefaultChannelHandlerContext.fireChannelRead(Object) line: 326	
	StringDecoder(MessageToMessageDecoder<I>).channelRead(ChannelHandlerContext, Object) line: 103	
	DefaultChannelHandlerContext.invokeChannelRead(Object) line: 340	
	DefaultChannelHandlerContext.fireChannelRead(Object) line: 326	
	DelimiterBasedFrameDecoder(ByteToMessageDecoder).channelRead(ChannelHandlerContext, Object) line: 155	
	DefaultChannelHandlerContext.invokeChannelRead(Object) line: 340	
	DefaultChannelHandlerContext.fireChannelRead(Object) line: 326	
	LoggingHandler.channelRead(ChannelHandlerContext, Object) line: 283	
	DefaultChannelHandlerContext.invokeChannelRead(Object) line: 340	
	DefaultChannelHandlerContext.fireChannelRead(Object) line: 326	
	DefaultChannelPipeline.fireChannelRead(Object) line: 785	
	AbstractNioByteChannel$NioByteUnsafe.read() line: 129	
	NioEventLoop.processSelectedKey(SelectionKey, AbstractNioChannel) line: 494	
	NioEventLoop.processSelectedKeysOptimized(SelectionKey[]) line: 461	
	NioEventLoop.processSelectedKeys() line: 378	
	NioEventLoop.run() line: 350	
	SingleThreadEventExecutor$2.run() line: 101	
	Thread.run() line: 745	




内存泄露检测

 protected static ByteBuf toLeakAwareBuffer(ByteBuf buf) {
        ResourceLeak leak;
        switch (ResourceLeakDetector.getLevel()) {
            case SIMPLE:
                leak = AbstractByteBuf.leakDetector.open(buf);
                if (leak != null) {
                    buf = new SimpleLeakAwareByteBuf(buf, leak);
                }
                break;
            case ADVANCED:
            case PARANOID:
                leak = AbstractByteBuf.leakDetector.open(buf);
                if (leak != null) {
                    buf = new AdvancedLeakAwareByteBuf(buf, leak);
                }
                break;
        }
        return buf;
    }

上一篇     下一篇
select poll epoll 区别

netty ByteBuf vs java NIO ByteBuffer

netty高性能之道

netty代码debug切入点

java NIO selector

缓存雪崩穿透预热更新降级