Eclipse 2023-09: Strange Memory Leak Issue in a Spring Boot Application with WebSocket Handling
I'm following best practices but I'm relatively new to this, so bear with me. I'm experiencing a persistent memory leak in my Spring Boot application that heavily utilizes WebSockets. After deploying the app on Tomcat, I've noticed that the memory usage keeps increasing over time, even with no significant load. The application uses Spring WebSocket for real-time communication and is running on Eclipse 2023-09. I've already enabled the `-Xmx` and `-Xms` flags to monitor the heap size and confirmed that the heap is growing consistently every few minutes. Here's a simplified version of my WebSocket configuration: ```java import org.springframework.context.annotation.Configuration; import org.springframework.messaging.simp.config.MessageBrokerRegistry; import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker; import org.springframework.web.socket.config.annotation.StompEndpointRegistry; import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer; @Configuration @EnableWebSocketMessageBroker public class WebSocketConfig implements WebSocketMessageBrokerConfigurer { @Override public void configureMessageBroker(MessageBrokerRegistry config) { config.enableSimpleBroker("/topic"); config.setApplicationDestinationPrefixes("/app"); } @Override public void registerStompEndpoints(StompEndpointRegistry registry) { registry.addEndpoint("/ws").setAllowedOrigins("*").withSockJS(); } } ``` In my `@Controller`, I handle messages like this: ```java import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Controller; @Controller public class MessageController { @MessageMapping("/sendMessage") @SendTo("/topic/messages") public Message sendMessage(Message message) throws Exception { // Simulate processing return message; } } ``` I also verified that I am properly closing any resources and not holding onto sessions longer than necessary. In my tests, I used VisualVM and found that a lot of memory is being retained by `org.springframework.web.socket.messaging.SubProtocolWebSocketHandler` instances. I tried changing the WebSocket messaging configuration to use a more robust broker like RabbitMQ, but the issue persists. Additionally, I'm using Java 17, and all dependencies are up to date. Has anyone faced similar issues? Any suggestions on how to investigate or fix this memory leak? Logs show nothing suspicious, and I'm not seeing any exceptions thrown during runtime. My team is using Java for this desktop app. I'd love to hear your thoughts on this. I'm working on a application that needs to handle this. Any ideas what could be causing this?