I’m currently developing a java package similar to .net signalr that utilizes netty. As I tried to benchmark it I realized that there’s a memory leak in my application. I narrowed down my search to the section where I write the result of the client request back to the TCP channel. Code below is where the memory leak is happening.
private ChannelFuture writeMessageInternal(Message message) {
ByteBuf buf;
Codec codec = message.getCodec();
try {
buf = codec.encode(message);
} catch (Exception e) {
throw new RuntimeException();
}
ChannelFuture channelFuture = channel.writeAndFlush(buf);
return channelFuture;
}
If I comment ChannelFuture channelFuture = channel.writeAndFlush(buf);
and change the code like:
private ChannelFuture writeMessageInternal(Message message) {
ByteBuf buf;
Codec codec = message.getCodec();
try {
buf = codec.encode(message);
} catch (Exception e) {
throw new RuntimeException();
}
// ChannelFuture channelFuture = channel.writeAndFlush(buf);
return null;
}
then no memory leak happens. buf
is an unpooled bytebuf. So it didn’t require to be released manually.
Currently java 21 and netty-all of version 4.1.58.Final is benig used.
Arshiya Kiani is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.