JSF 2.1 ViewScopedBean @PreDestroy method is not called

后端 未结 2 1496
我寻月下人不归
我寻月下人不归 2020-12-14 23:37

I have a method in a view Scoped Bean with the @PreDestroy annotation and another one with the @PostConstruct annotation.

The @PostCo

2条回答
  •  伪装坚强ぢ
    2020-12-14 23:57

    I have prepared a small NetBeans project demonstrating when JSF2.2 CDI-compatible @ViewScoped beans (javax.faces.view.ViewScoped) are released for garbage collection under different navigation cases (for Mojarra 2.2.9, Glassfish4, NetBeans8.0.2, JDK1.7), available for download here. The code is omitted here, please see that download.

    The navigation cases handled and the results are summarised by this image:

    Image showing index page using @ViewSCoped bean with JSF navigation cases to a done landing page

    To monitor the @ViewScoped beans, use VisualVM against Glassfish (or the in-built NetBeans profiler on the mini project) and filter the Sampler memory Heap histogram class view on the package name 'webel.com.jsf'. The following image shows an absurd 66 instances of webel.com.jsf.Jsf22ViewBean after copious experimenting with h:link, browser URL GETs, and browser RELOAD GETs, which instances will not be garbage collected (which you can test using the VisualVM Perform GC button):

    enter image description here

    By comparison, navigating away from the index.xhtml (which uses the @ViewScoped bean once for a simple EL variable read) to done.xhtml (which does not use the bean at all) using h:commandButton and an action method expression or an action string results in the @ViewScoped bean being released for garbage collection (there is always one reference from the WeldClientProxy to a @ViewScoped bean, and as you navigate back and forth using h:commandButton the WeldClientProxy moves from one releasable bean to the next).

提交回复
热议问题