Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in WebSocketImpl.outQueue #773

Closed
martin-g opened this issue Sep 11, 2018 · 6 comments · Fixed by #781
Closed

Memory leak in WebSocketImpl.outQueue #773

martin-g opened this issue Sep 11, 2018 · 6 comments · Fixed by #781

Comments

@martin-g
Copy link

Hi,

This is not a bug report or feature request but actually I'd like to ask for help debugging a possible memory leak.

In the following screenshot you can see that ~51% of the used heap is kept by WebSocketWorker and WebSocketImpl.

51-percents-in-websocketserver

And here we see that actually WebSocketImpl#outQueue (with hashcode 0x7127e8b30) is the one keeping the memory.
outqueue

Its size is 25320:
queue-size-25320

Debugging the code I've seen that outQueue is populated when I use org.java_websocket.WebSocket#send(java.lang.String) method. At that time there is a check whether the connection is still opened and WebsocketNotConnectedException is being thrown in case it is not. Then org.java_websocket.WebSocketImpl#write(java.nio.ByteBuffer) is called where there the buf is just put into outQueue.
Later in the run loop of org.java_websocket.server.WebSocketServer#run() the outQueue must be drained and actually sent to the client. In case of an error an IOException is thrown and org.java_websocket.server.WebSocketServer#onClose(WebSocket, int, String, boolean) callback is called.

One thing that I notice is that neither org.java_websocket.server.WebSocketServer#handleIOException() nor org.java_websocket.WebSocketImpl#closeConnection(int, java.lang.String, boolean) clear the contents of inQueue and outQueue. But I am not sure whether this is the reason for the huge out queues I have in the heap dump.

Do you have an idea what could be the problem and what I could do to debug more ?

I use version 1.3.8. I see that 1.3.9 is released and I will upgrade to it but I do not see anything that might fix it in the changelog.

For the heap analysis I use Eclipse Memory Analyzer (v 1.8.0).

Thank you!

@marci4
Copy link
Collaborator

marci4 commented Sep 12, 2018

Hello @martin-g,

thank you for your bug report!

Running everything with the profiler, I saw some left over references to WebSocketImpl instances.

The problem is in my eyes, that the WebSocketWorker is referencing an instance but will still keep it until a new WebSocketImpl instance is put into the queue and therefore gc cannot clean up everything.

I attached a patched version. Could you please check if this solves the issue. If not I may need additional steps to repeat!
Java-WebSocket-1.3.10-SNAPSHOT.zip

Best regards,
marci4

@martin-g
Copy link
Author

Thank you, @marci4 !
I will test it soon!

Could you please push your changes in a branch so I can see them ?

@marci4
Copy link
Collaborator

marci4 commented Sep 12, 2018

Hello @martin-g,

you can find it here. Just merged it locally back to 1.3.9 for you.

Best regards,
marci4

@marci4
Copy link
Collaborator

marci4 commented Sep 21, 2018

@martin-g any news?

@martin-g
Copy link
Author

I will take some heap dumps and take a look later today!

@martin-g
Copy link
Author

Hi @marci4,

Everything looks in today's heap dump!
Thank you!

@marci4 marci4 added this to the Release 1.4.0 milestone Sep 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants