Bug 59189

Summary: PerMessageDeflate leaves native memory allocated unnecessarily
Product: Tomcat 7 Reporter: Henrik Olsson <henrik.olsson>
Component: WebSocketAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 7.0.68   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Attachments: Makes PerMessageDeflate call end on Inflater/Deflater by adding a close() method to the Transformation interface

Description Henrik Olsson 2016-03-17 11:09:52 UTC
During websocket load tests we noticed that the resident memory usage of the tomcat process would grow and stay in use even after the sockets were closed when websocket compression was enabled. The java heap looked fine. It seems to be caused by PerMessageDeflate creating a Deflater and Inflater which in turn allocates native memory that isn't freed until the JVM decides to run their finalizers, which can take a long time before it happens.

I propose calling .end() (which is what finalize() does) on the Deflater and Inflater when the websocket is closed. I have created a patch that does this and resident memory usage shrinks after the websockets are closed down with it. Would you be interested in this?
Comment 1 Mark Thomas 2016-03-17 11:15:58 UTC
Sure. Attach the patch to this issue and someone will take a look.
Comment 2 Henrik Olsson 2016-03-17 12:08:34 UTC
Created attachment 33682 [details]
Makes PerMessageDeflate call end on Inflater/Deflater by adding a close() method to the Transformation interface
Comment 3 Henrik Olsson 2016-03-17 12:09:10 UTC
(In reply to Mark Thomas from comment #1)
> Sure. Attach the patch to this issue and someone will take a look.

Attachment added. Not sure if it's the best way to do it but at least it's something :)
Comment 4 Mark Thomas 2016-03-18 11:34:11 UTC
Thanks for the report and the proposed fix.

I ended up applying a variation on your patch since I found a simpler path to call close().

The fix has been applied to:
- 9.0.x for 9.0.0.M5 onwards
- 8.5.x for 8.5.1 onwards
- 8.0.x for 8.0.33 onwards
- 7.0.x for 7.0.69 onwards
Comment 5 Henrik Olsson 2016-03-18 11:41:31 UTC
(In reply to Mark Thomas from comment #4)
> Thanks for the report and the proposed fix.
> 
> I ended up applying a variation on your patch since I found a simpler path
> to call close().
> 
> The fix has been applied to:
> - 9.0.x for 9.0.0.M5 onwards
> - 8.5.x for 8.5.1 onwards
> - 8.0.x for 8.0.33 onwards
> - 7.0.x for 7.0.69 onwards

Thanks for the quick response!