question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Apparent memory leak when writing a TextWebSocketFrame on websocket connect.

See original GitHub issue

Expected behavior

I’m trying to write a Netty based Web Socket server that expected to receive high load. The basic reproducible case involves simply opening a web socket connection and the server sending back a welcome message to the client.

Actual behavior

While load testing during development, I’m seeing excessive memory utilization by the process that cannot be attributed to any identifiable source (at least, not identifiable by me, thus the issue). Under heavy load, the process eventually consumes all available memory on the host before it is killed by a separate oom-reaper process.

This memory utilization only occurs when the server starts writing new, non-empty,TextWebSocketFrames.

Steps to reproduce

Modify the example Web Socket server to send back any non-empty greeting on connect and send heavy load. (Note: the definition of heavy load will vary based on the available memory for the service).

Minimal yet complete reproducer code (or URL to code)

Sample addition to WebSocketFrameHandler

  @Override
  public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    super.userEventTriggered(ctx, evt);
    if (evt instanceof HandshakeComplete) {
      websocketConnected(ctx);
    }
  }

  private void websocketConnected(ChannelHandlerContext ctx) {
    ctx.channel().writeAndFlush(new TextWebSocketFrame("{}"));
  }

For a fully functioning app that should exhibit the behavior in question, see this repo: https://github.com/jcohen/netty-ws-leak

Netty version

4.1.43.Final

JVM version (e.g. java -version)

openjdk version "1.8.0_212"
OpenJDK Runtime Environment (IcedTea 3.12.0) (Alpine 8.212.04-r0)
OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)

OS version (e.g. uname -a)

Linux a1d94bdbf936 4.9.184-linuxkit #1 SMP Tue Jul 2 22:58:16 UTC 2019 x86_64 Linux

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
normanmaurercommented, Nov 26, 2019

@jcohen alright I did spent some time to look into it and sadly there is not much we can do on our side in netty…

The problem is that with compression enabled it will use permessage-deflate with server_no_context_takeover not used. For more infos on what this actual means check: See https://tools.ietf.org/html/rfc7692#section-7.1.1.1.

So with the configuration we need to create a new Deflater (which is part of the JDK: https://docs.oracle.com/javase/8/docs/api/java/util/zip/Deflater.html ) and keep it “alive” till the connection is closed. The problem here is that Deflater is implemented using JNI and so reserve native memory that will not be released until the connection is closed and so the deflater.end() method can be called by us.

So the only way to “guard” against such a problem is to limit the max number of concurrent connections which use the extension.

2reactions
normanmaurercommented, Nov 25, 2019

Ok thanks … looking into it now

Am 25.11.2019 um 17:26 schrieb Joshua Cohen notifications@github.com:

Well that was an easy fix! Memory utilization is fine with WebSocketServerCompressionHandler removed from the pipeline!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Finding WebSocket Server memory leak - Stack Overflow
Hello guys i am currently testing a websocket server written in Asp .Net Core 2.0 and i believe i have a memory leak....
Read more >
63306 – Memory leak during websocket connection close
What I observe is: - Tomcat continues to be able to write to the client without any apparent error - There is (eventually)...
Read more >
Baffling Apparent Native Memory Leak - Google Groups
messages and close the websocket session) I appear to be leaking a massive amount of native memory. (guesstimate of 50-100K bytes per connection)....
Read more >
4.1-dockerimages - Cisco
This document contains licenses and notices for open source software used in this product. With respect to the free/open source software listed in...
Read more >
Netty in Action MEAP v05
how Netty addresses Java issues, such as the Epoll bug or memory leaks. ... Netty simplifies network programming of TCP or UDP servers...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found