How and when is a @ViewScoped bean destroyed in JSF?
Asked Answered
S

1

17

The lifecycle of the @RequestScoped and @SessionScopedBean managed beans are managed by the Servlet container itself since they are basically stored as an attribute of HttpRequest and HttpSession respectively. How do JSF manage the lifecycle of the @ViewScopedBean ? I know it gets created when the view is created and is usable till there is a postback to a different view. But I found out that is not garbage collected immediately after we move from that view.

Ss answered 7/3, 2013 at 7:29 Comment(3)
Once a managed bean action method returns a valid navigation case outcome, even though it's to the same view, the view scoped managed bean will be garbaged and recreated. Read this balusc.blogspot.co.il/2011/09/communication-in-jsf-20.html and this is where they stored : https://mcmap.net/q/17401/-how-to-get-beans-from-view-scopeShaer
Well, in reality, you're not supposed to be able to predict gc or have it happen immediately are you? But regarding the viewscoped beans in mojarra, see https://mcmap.net/q/17402/-linked-viewscoped-beans-lead-to-memory-leaksNubilous
garbage collection is asynchronous. when the view scope is "destroyed" it is really just "marked ready to be garbage collected", just like all other java objects. Note: the marking is really a reference count of 0 (i.e. marked "no longer used").Libyan
S
34

It will be destroyed when

  • a postback with a non-null outcome is been performed,

  • or, the number of (logical) views in session has exceeded and the particular view is the first one in LRU chain (in Mojarra, that's configureable by com.sun.faces.numberOfViewsInSession and com.sun.faces.numberOfLogicalViews context parameters, each with a default value of 15),

  • or, the number of active view scopes in session has exceeded (in Mojarra, that's a hardcoded limit of 25), see also JSF 2.2 Memory Consumption: Why does Mojarra keep the ViewScoped Beans of the last 25 Views in Memory?

  • or, the session has expired.

It will thus not be destroyed when the page is unloaded as result of clicking a GET link to another page, or refreshing the page, or closing the browser tab/window. The bean will live as long until one of abovelisted conditions is met. To destroy it during unload anyway, consider using OmniFaces @ViewScoped instead.

Spoon answered 13/3, 2013 at 16:47 Comment(7)
what you mean by LRU?Least recently used ? and also what do you mean by logical views?Ss
1) Yes. They're stored in a LRU map, ordered by last usage. The least recently used is the 1st entry. 2) See stackoverflow.com/questions/12562469/…Spoon
@BalusC, are there any solutions to destory viewscoped beans faster ? will ominfaces viewscoped annotation help with that ?Keyway
in icefaces there is one annotation @WindowDisposed.on browser close itself the view will get garbage collectedDantedanton
Maybe add some info about the js to immediately do it on 'unload' ?Ragout
@BalusC,is something like the following will force destroy for view scope bean : FacesContext.getCurrentInstance().getViewRoot().getViewMap().clear(); for example after click submit button which will process request and after that success message should appear to the userKeyway
@Mahmoud: Nope. View scoped beans are stored in session.Spoon

© 2022 - 2024 — McMap. All rights reserved.