Bug 65809 - Reduce memory footprint for long-lasting WebSocket server connections
Summary: Reduce memory footprint for long-lasting WebSocket server connections
Status: NEW
Alias: None
Product: Tomcat 9
Classification: Unclassified
Component: WebSocket (show other bugs)
Version: unspecified
Hardware: PC Linux
: P2 enhancement with 5 votes (vote)
Target Milestone: -----
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2022-01-19 16:31 UTC by Maurizio Adami
Modified: 2024-07-03 16:34 UTC (History)
1 user (show)


Note You need to log in before you can comment on or make changes to this bug.
Description Maurizio Adami 2022-01-19 16:31:50 UTC
I'm using a WebSocket service that manage a large amount of open connections, sending back messages to clients.
The number of concurrent messages can be negligible if compared to the number of active connections.
In such a situation I've found the running Tomcat process can use approx. 100KB of memory per open WebSocket connection.
Looking at source code I've found some classes with buffers allocated inside the constructor.

With the default value of 8192 for org.apache.tomcat.websocket.DEFAULT_BUFFER_SIZE:
In org.apache.tomcat.websocket.WsFrameBase
 - inputBuffer 8KB
 - messageBufferBinary 8KB
 - messageBufferText 16KB
In org.apache.tomcat.websocket.WsRemoteEndpointImplBase
 - outputBuffer 8KB
 - encoderBuffer 8KB

With the above buffers this sum up to a 48KB of memory per WebSocket connection.
Changing the allocation strategy to an on-demand buffer allocation, in the above situation
could reduce the memory footprint by 480MB of memory for 10K active connections.

The buffers could also be pooled by a pool manager, reducing the allocation costs.
Comment 1 Mark Thomas 2024-07-03 16:28:54 UTC
Tomcat 8 has reached End Of Life. Moving this bug to Tomcat 9.
Comment 2 Mark Thomas 2024-07-03 16:34:05 UTC
Correct component after move to Tomcat 9.