Memory implications of OmniFaces ViewScoped bean?
Asked Answered
D

1

4

From what I understand, ViewScoped beans only get destroyed when one of the following take place:

1) JSF sends a POST request to another page with something like a <h:commandLink...>

2) The number of open beans exceeds the maximum threshold setting (default of 15)

3) The user's session expires

Here is my confusion:

Does #1 mean that if a user navigates away from the page with a GET request, the bean will stay open, even if eventually a JSF POST happens in the same browser tab on another page? Or will all active @ViewScoped instances for that browser tab be destroyed once a JSF POST is sent regardless of which page the user is on?

Does #2 mean that a user can have 15 bean instances active for each @ViewScoped class? Or is it 15 bean instances regardless of class -- meaning I could have 5 instances of Class1, 5 instances of Class2, and 5 instances of Class3, and a new bean would destroy the oldest active bean?

For #3, if STATE_SAVING_METHOD is set to "client", will that have any implications in ViewScoped beans being destroyed? From what I remember, there needs to be a way to manually control session expiration if STATE_SAVING_METHOD is set to client.

Finally, is there a way to manage active ViewScoped beans so that they can be destroyed when a user clicks "logout" for instance?

Dragging answered 20/1, 2014 at 14:39 Comment(9)
This is going to be a long story, but in a nutshell it's not different from how JSF's own @ViewScoped works.Scrutiny
For your last question about managing @ViewScoped beans, I can't see any reason for doing such an operation if session removing guarantees they'll be deleted.Hyson
I guess that last question is there in case the beans do not get destroyed on session expiration when STATE_SAVING_METHOD is set to client. My main concern is that I'm storing too much information in ViewScoped beans (such as table data) that's not being destroyed. I want to be sure I'm using this scope properly. It seems like ViewScoped might've simply been designed for pages with forms that get destroyed with a submit button, but it's also handy for being able to cache calculated data, so long as memory problems don't arise from using it.Dragging
Just use JPA to deal with entities and leverage caching job to it. Remember, in Java you aren't storing copies of value in memory, but of reference.Scrutiny
@BalusC, then when to use omnifaces viewscope instead of jsf viewscope ?Rules
@MahmoudS: that's explained in its javadoc and showcase.Scrutiny
@@Scrutiny i read it before asking, honestly it's not clear documentation.Rules
@Scrutiny Is it correct that the OmniFaces ViewScoped is more aggressive about destroying expired ViewScoped beans, as I observed here: stackoverflow.com/questions/20930396/…Metathesis
@Jon: Yes, that's correct. That's also explained in Javadoc.Scrutiny
D
2

I figured out the answers to these by adding a @PreDestroy method to each @ViewScoped bean, and logging when it gets destroyed. For others who might be curious about this:

For #1, a bean does not get destroyed if you navigate away from the page with a GET request, but then send a post request at a later time. That bean will stay in memory until the "maximum active view scopes" setting has been reached and it's the bean's turn to be destroyed, or the session has been invalidated.

For #2, class doesn't matter. You can have 5 instances of Class1, 5 instances of Class2, and 5 instances of Class3, and a new ViewScoped bean instance will destroy the oldest bean, given your threshold is 15.

For #3, it looks like the beans are destroyed once the session has been invalidated even if STATE_SAVING_METHOD is set to client.

Dragging answered 21/1, 2014 at 16:0 Comment(1)
Thanks for your interesting question and analysis. You remarks don't always distinguish between "destroyed" in the sense of @PreDestroy methods being invoked and the ability to garbage collect the view beans themselves. I have asked a related question here stackoverflow.com/questions/40569971/… and have made available via GitHub a test web app for easily comparing @PreDestroy and heap references (garbage collectability) for various @ViewScoped bean types: github.com/webelcomau/JSFviewScopedNavCavell

© 2022 - 2024 — McMap. All rights reserved.