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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Geek
  • 26,489
  • 43
  • 149
  • 227
  • 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 http://balusc.blogspot.co.il/2011/09/communication-in-jsf-20.html and this is where they stored : http://stackoverflow.com/a/9177769/617373 – Daniel Mar 07 '13 at 07:33
  • 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 http://stackoverflow.com/a/13097208/1530938 – kolossus Mar 07 '13 at 12:15
  • 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"). – DwB Mar 13 '13 at 16:54

1 Answers1

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.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • what you mean by LRU?Least recently used ? and also what do you mean by *logical* views? – Geek Mar 13 '13 at 17:15
  • 2
    1) Yes. They're stored in a LRU map, ordered by last usage. The least recently used is the 1st entry. 2) See http://stackoverflow.com/questions/12562469/how-can-i-set-the-view-timeout/12564632#12564632 – BalusC Mar 13 '13 at 17:18
  • @BalusC, are there any solutions to destory viewscoped beans faster ? will ominfaces viewscoped annotation help with that ? – Mahmoud Saleh Sep 27 '14 at 10:04
  • 1
    in icefaces there is one annotation @WindowDisposed.on browser close itself the view will get garbage collected – Vishnudev K Oct 30 '14 at 13:18
  • Maybe add some info about the js to immediately do it on 'unload' ? – Kukeltje Jan 11 '17 at 19:17
  • @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 user – Mahmoud Saleh Jun 09 '21 at 00:43
  • @Mahmoud: Nope. View scoped beans are stored in session. – BalusC Jun 09 '21 at 08:57