Direct Buffer OOM #1004

Closed
coltnz opened this Issue Jan 31, 2013 · 3 comments

Projects

None yet

3 participants

@coltnz
Contributor
coltnz commented Jan 31, 2013

This code runs fine on the 4alpha but not beta snapshot unless I switch directBuffer() to buffer() not a big deal obviously but I thought it might be significant.

    private void sendError(@Nonnull ChannelHandlerContext ctx, @Nonnull HttpResponseStatus status, @Nonnull String msg) {
        final HttpResponse response = new DefaultHttpResponse(HTTP_1_1, status);
        if (msg != null) {
            final ByteBuf msgBuffer = Unpooled.directBuffer(msg.getBytes().length);
            msgBuffer.setBytes(0, msg.getBytes(RequestUtil.CHARSET));
            response.setContent(msgBuffer);
        }
        sendMsg(ctx, response, false);
    }
17:11:34.974 [nioEventLoopGroup-2-2] WARN  i.n.channel.DefaultChannelPipeline:103 - An exceptionCaught() event was fired, and it reached at the end of the pipeline.  It usually means the last inbound
io.netty.channel.ChannelPipelineException: java.lang.OutOfMemoryError: Direct buffer memory
    at io.netty.channel.DefaultChannelPipeline.notifyHandlerException(DefaultChannelPipeline.java:1396) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelHandlerContext$8.run(DefaultChannelHandlerContext.java:205) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelHandlerContext.callFreeOutboundBuffer(DefaultChannelHandlerContext.java:1172) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelHandlerContext.access$500(DefaultChannelHandlerContext.java:39) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelHandlerContext$7.run(DefaultChannelHandlerContext.java:184) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:225) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.socket.nio.NioEventLoop.run(NioEventLoop.java:297) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:110) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_08-ea]
Caused by: java.lang.OutOfMemoryError: Direct buffer memory
    at java.nio.Bits.reserveMemory(Bits.java:658) ~[na:1.7.0_08-ea]
    at java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:123) ~[na:1.7.0_08-ea]
    at java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:306) ~[na:1.7.0_08-ea]
    at io.netty.buffer.PoolArena$DirectArena.newChunk(PoolArena.java:362) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.buffer.PoolArena.allocateNormal(PoolArena.java:149) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.buffer.PoolArena.allocate(PoolArena.java:138) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.buffer.PoolArena.allocate(PoolArena.java:88) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.buffer.PooledByteBufAllocator.newDirectBuffer(PooledByteBufAllocator.java:141) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:92) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:83) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.buffer.PooledByteBufAllocator.ioBuffer(PooledByteBufAllocator.java:146) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelPipeline$HeadHandler.newOutboundBuffer(DefaultChannelPipeline.java:1482) ~[netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelHandlerContext.initOutboundBuffer(DefaultChannelHandlerContext.java:348) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelHandlerContext.lazyInitOutboundBuffer(DefaultChannelHandlerContext.java:328) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelHandlerContext.hasOutboundByteBuffer(DefaultChannelHandlerContext.java:515) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
    at io.netty.channel.DefaultChannelHandlerContext$8.run(DefaultChannelHandlerContext.java:195) [netty-4.0.0.Beta1-SNAPSHOT.jar:na]
@lw346
Member
lw346 commented Jan 31, 2013

What are your direct buffer vs heap memory settings?

I once saw this error arise in Apache HttpClient's use of direct buffers for SSL - the direct buffer was referenced by an item in the heap that hadn't yet been garbage collected as there was plenty of young gen capacity still. As a result, I saw the direct memory OOM.

This is not really a bug in netty.

@trustin
Member
trustin commented Mar 5, 2013

@coltnz, could you specify the following option when you launch a JVM:

-Dio.netty.resourceLeakDetection

Let me know if Netty warns you about any leak.

@trustin
Member
trustin commented Mar 6, 2013

I think this should be fixed, now that we release the content of an HTTP object correctly. Please reopen if it still fails for you. Also, I'd use ctx.alloc().directBuffer() if you prefer using a direct buffer.

@trustin trustin closed this Mar 6, 2013
@trustin trustin was assigned Mar 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment