lifecycle and GC

Hi folks,

I have a generic, simple, but very important question. ( I am really new on Vaadin)
if there is a realy good explanation somewhere, please accept my sorry.

I am really curious about perspective of gc. and I am about to start a project which will have several views in it.

thinking that one view will have several objects in it like buttons, view, containers which holds several ORM objects.

how can I be sure that after changing one view to another, the objects bound the ex-view will be garbaged at some time.

I can not be sure that these objects will be perform of gc.

is there a best practice document explains this topic from point of GC

regards

Hi Faruk,

Good question, definitely something you want to keep in mind when developing to avoid unecessary mistakes.

A good practice to keep memory consumption in check in a Vaadin application is that when you register views to your navigator, you do so by registering the class, not an instance.

navigator.addView(StatusView.class); // A new instance is always created when navigating to the view, and the old instance is eligible for GC when you have navigated away
navigator.addView(new StatusView()); // Same instance is returned every time, so a reference to the view must be kept. This prevents it from being garbage collected until the UI is garbage collected

Vaadin components themselves are pretty lightweight, so you’re not likely to run into big issues by just keeping them in memory. What you want to look out for is any large in-memory containers as they often take up multiple times more memory than just the data contained in them. If you have large data sets and a lot of users, consider using lazy loading containers.

Other than that, follow normal good practices and don’t use wider scopes than needed for variables.

Hi Marcus,

thank you for your answer. this is the point I was looking for.

navigator.addView(StatusView.class); // A new instance is always created when navigating to the view, and the old instance is eligible for GC when you have navigated away
navigator.addView(new StatusView()); // Same instance is returned every time, so a reference to the view must be kept. This prevents it from being garbage collected until the UI is garbage collected