Hello! Why I got a memory leak here?
Let’s take my-starter-project
as a base.
application.properties:
vaadin.heartbeatInterval=5
server.servlet.session.timeout=60
MainView.java:
private static String generateRandomString(java.util.Random rnd, int length){
// Return some string of size `length`
...
}
@Route
@UIScope
@Push(transport = Transport.LONG_POLLING)
public class MainView extends VerticalLayout {
...
public MainView(@Autowired MessageBean bean) {
Button button = new Button("Click me",
e -> {
Random rnd = new Random();
for (int i = 0; i < 50; i++) {
Div div = new Div();
div.setText(generateRandomString(rnd, 1024 * 10));
this.add(div);
}
Notification.show(bean.getMessage());
});
add(button);
}
The above code outputs to browser window 50 lines of 10Kb each, no issues. Now, let’s replace the constructor’s code.
public MainView(@Autowired MessageBean bean) {
Random rnd = new Random();
for (int i = 0; i < 50; i++) {
Div div = new Div();
div.setText(generateRandomString(rnd, 1024 * 10));
this.add(div);
}
}
The above code has an issue: a memory leak – several objects org.apache.tomcat.utils.threads.TaskThread
are never destroyed (even after MainView objects count and SpringVaadinSession objects count become 0 due to session expiration). It is not related to servlet container, I tried all three available in Spring Boot: Tomcat, Jetty and Undertow. I attached a screenshot. It contains a string with Push data of AtmospherePushConnection (start with <!doctype html>...
).