Limit memory consumption of Vaadin session

In Vaadin Flow web apps, the state of the entire user-interface is maintained in the session on the web server, with automatic dynamic generation of the HTML/CSS/JavaScript needed to represent that UI remotely on the web browser client. Depending on the particular app, and the number of users, this can result in a significant amount of memory used on the web container.

Is it possible to limit the amount of memory a session and requests related to it can use?

For example, I would like to limit each user session to one megabyte. This limit should apply to any objects created when handling requests. Is that possible?

Answer

It is theoretically possible, but it is not practical.

As far as I am aware, no JVM keeps track of the amount of memory that (say) a thread allocates. So if you wanted to do this, you would build a lot of infrastructure to do that. Here are a couple of theoretical ideas.

  • You could use bytecode engineering to inject some code before each new to measure and record the size of the object allocated. You would need to run this across your entire codebase … including any Java SE classes and 3rd-party classes that you app uses.

  • You could modify the JVM to record the information itself. For example, you might modify the memory allocator that new uses.

However, both of these are liable be a lot of work to implement, debug and maintain. And both are liable to have significant performance impact.


It is not clear to me why you would need this … as a general thing. If you have a problem with the memory usage of particular types of requests, then it would be simpler for the request code itself to keep tabs on how big the request data structures are getting. When the data structures get too large, the request could “abort” itself.

Leave a Reply

Your email address will not be published. Required fields are marked *