NFO: [id: 0x9a25ff4f, L:/0:0:0:0:0:0:0:0:8080] RECEIVED: [id: 0x186eb870, L:/127.0.0.1:8080 - R:/127.0.0.1:36318]
io.netty.util.internal.OutOfDirectMemoryError: failed to allocate 1024 byte(s) of direct memory (used: 7456947207, max: 7456948224)
at io.netty.util.internal.PlatformDependent.incrementMemoryCounter(PlatformDependent.java:523)
at io.netty.util.internal.PlatformDependent.allocateDirectNoCleaner(PlatformDependent.java:477)
at io.netty.buffer.UnpooledUnsafeNoCleanerDirectByteBuf.allocateDirect(UnpooledUnsafeNoCleanerDirectByteBuf.java:30)
at io.netty.buffer.UnpooledUnsafeDirectByteBuf.<init>(UnpooledUnsafeDirectByteBuf.java:67)
at io.netty.buffer.UnpooledUnsafeNoCleanerDirectByteBuf.<init>(UnpooledUnsafeNoCleanerDirectByteBuf.java:25)
at io.netty.buffer.UnsafeByteBufUtil.newUnsafeDirectByteBuf(UnsafeByteBufUtil.java:425)
at io.netty.buffer.UnpooledByteBufAllocator.newDirectBuffer(UnpooledByteBufAllocator.java:65)
at io.netty.buffer.AbstractByteBufAllocator.directBuffer(AbstractByteBufAllocator.java:177)
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.44.Final</version>
</dependency>
Code Block :
public class HttpHelloWorldServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
HttpRequest req = (HttpRequest) msg;
if (HttpHeaders.is100ContinueExpected(req)) {
ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE));
}
boolean keepAlive = HttpHeaders.isKeepAlive(req);
ByteBuf responseBytes = ctx.alloc().buffer();
responseBytes.writeBytes("SUCCESS".getBytes());
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , responseBytes);
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
response.headers().set(CONTENT_LENGTH, response.content().readableBytes());
if (!keepAlive) {
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set(CONNECTION, Values.KEEP_ALIVE);
ctx.writeAndFlush(response);
}
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
Server:
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_BACKLOG, 1024);
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new SinkInitializer(sslCtx));
b.childOption(ChannelOption.SO_KEEPALIVE, true);
Channel ch = b.bind(PORT).sync().channel();
System.err.println("Open your web browser and navigate to " +
(SSL? "https" : "http") + "://127.0.0.1:" + PORT + '/');
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
Initializer :
ChannelPipeline p = ch.pipeline();
p.addLast("codec", new HttpServerCodec());
p.addLast("aggregator", new HttpObjectAggregator(Short.MAX_VALUE));
p.addLast("handler",new HttpHelloWorldServerHandler());
What could be the reason ??
While running im using this param ==> -Dio.netty.recycler.maxCapacity=0 .
Appreciate your input here :-) ..
Cheers,
Senthil