How to Handle Vaadin Sessions and Memory in Production
Keep Vaadin server memory in check: what lives in the VaadinSession and UI, how heartbeats close dead UIs, what to put in grids and how to find leaks.

Vaadin Flow keeps the state of every open view on the server. That is the reason the programming model is so simple, and the reason a Vaadin application is sized by concurrent sessions, not by requests per second. Memory is the resource that runs out first. This post uses Vaadin 25 on Spring Boot 4 and JDK 25, and the mechanics are in how the Flow server-side model works.
What lives where
| Object | One per | Holds |
|---|---|---|
VaadinSession | User session | Locale, attributes, the list of UIs |
UI | Browser tab | The component tree and navigation state |
| View components | Inside a UI | Fields, grids and whatever data you loaded into them |
Two tabs from the same user are two UIs in one session, so memory grows with tabs as well as users. Closing a tab does not notify the server in a reliable way, which is why Vaadin uses heartbeats to decide a UI is gone.
Heartbeats and timeouts
The browser sends a small heartbeat request on an interval, 300 seconds by default. A UI that misses three consecutive heartbeats is closed and its tree becomes garbage. The session itself ends on the servlet session timeout:
vaadin.heartbeatInterval=120
server.servlet.session.timeout=30mA shorter heartbeat frees memory from abandoned tabs sooner, at the cost of more small requests. There is a trap in the other direction: heartbeats keep a session alive, so a user who leaves a tab open overnight never times out. Setting vaadin.closeIdleSessions=true makes the session end after the timeout even when heartbeats continue, and that is what most internal tools want.
Work out a per-user figure
Do not guess how much a user costs. Open the application, navigate to the heaviest view in one tab, force a garbage collection and take a heap dump (jcmd <pid> GC.heap_dump file.hprof). In a heap analyser, find the UI instance and read its retained size. Suppose the retained size comes to 2 MB. Then 500 users with two tabs each need about 2 GB for UIs alone, before the framework, caches and the rest of the application. That arithmetic, with your own number, is the sizing exercise. Do it for the heaviest realistic view, not the login page.
Keep the tree small
The usual cost is data, not components. Pass a List of 50,000 items to a Grid and that list stays in the session as long as the view does. Fetch pages on demand instead:
@Route("orders")
class OrderView extends VerticalLayout {
OrderView(OrderRepository orders) {
var grid = new Grid<Order>(Order.class);
grid.setItems(query -> orders.findAll(
PageRequest.of(query.getPage(), query.getPageSize()))
.stream());
add(grid);
}
}The callback runs for each request and keeps nothing between calls. Sorting and filtering need to be pushed into the query, otherwise the grid fetches everything to sort it. The details are in lazy loading grids.
Scope matters for beans too. A bean annotated @VaadinSessionScope lives as long as the session, and one that caches a large reference table per user multiplies that table by the user count. Make such data an ordinary singleton shared by everyone.
Release what you register
A listener registered on an application-wide object holds a reference back to your component for as long as that object lives. An event bus bean is the classic case:
private Registration registration;
@Override
protected void onAttach(AttachEvent event) {
registration = bus.subscribe(this::refresh);
}
@Override
protected void onDetach(DetachEvent event) {
registration.remove();
}Registering in onAttach and removing in onDetach ties the subscription to the time the component is in use. Registering in the constructor and never removing it keeps every view instance ever created alive. Scheduled tasks and push subscriptions follow the same rule, and server push updates shows the pattern for them.
Finding a leak
Take a heap dump of a node that has been running under load and filter the class list by com.vaadin.flow.component.UI. The count should be close to the number of open tabs. If it keeps growing between dumps, pick one UI that should be gone and ask the analyser for the path to GC roots. It usually ends at a static map, a singleton bean or a thread pool task. The general workflow is the same as debugging a memory leak.
Clustering needs more than memory
Running several nodes with session replication requires everything in the session to be Serializable, including your view state and any bean it references. Without replication, use sticky sessions on the load balancer, and accept that a node restart logs its users out. Sticky sessions are the simpler choice, and the right one unless the business requires users to survive a deployment.
Load testing and watching it in production
Do not trust the arithmetic alone; test it. Script a realistic user journey with a tool that speaks the Vaadin protocol, or with a browser-driven load tool, run it with the expected number of concurrent users and watch the heap. Take heap histograms at the start and after ten minutes, and check that the number of UI objects equals the number of simulated tabs and not more. Run the test long enough for sessions to time out, and confirm the memory comes back down. A service that grows steadily during a flat load test has a leak that a spreadsheet would not have found.
In production, export the number of active sessions and UIs as metrics. A custom gauge reading VaadinService session counts is a few lines, and the graph next to heap usage shows memory per user directly. Alert on heap after garbage collection, not on raw heap, since a healthy JVM fills its heap between collections. A rising floor means retained memory. The same metrics tell you when to add a node, which is a calmer way to find out than an OutOfMemoryError; the diagnosis of that error is in heap space errors.
Cutting the cost of each view
Look at what a view keeps. A ComboBox loaded with ten thousand items holds them all in the session; use a data provider with lazy loading and a filter. A cached Image or a StreamResource that holds a byte array of a generated report keeps the bytes until the resource is released, so generate on demand. Dialogs that are created once and kept hidden hold their entire tree for the life of the view; create them when needed and let them go. A view with a hundred components costs far less than a view with a hundred components and a ten-megabyte list of entities behind them.
When per-tab state is the wrong cost
Thousands of anonymous visitors each holding a server-side UI is the wrong model, since you pay memory for people who read one page and leave. Serve a public site with a stateless frontend and keep Vaadin for authenticated tools with a known user count. The case is argued in when Vaadin is the wrong choice. The Vaadin documentation describes session and UI initialisation, and the Spring Boot reference lists the session properties.


