Linked ViewScoped beans lead to memory leaks

[亡魂溺海] 提交于 2019-12-30 03:13:29

问题


In our JavaEE6 project (EJB3, JSF2) on JBoss 7.1.1, it seems we have a memory leak with @ViewScoped beans. Last tree days I've spent time on this issue investigation. So i've created simple project with two pages to guarantee that after first page leaving @ViewScoped bean will be released.

<context-param>  //web.xml
   <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
   <param-value>server</param-value>
</context-param>
<context-param>
   <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
   <param-value>false</param-value>
</context-param>

TreeBean.java

@ManagedBean
@ViewScoped
public class TreeBean implements Serializable {
 private TreeNode root;  
 public static AtomicInteger count = new AtomicInteger(0);

@Override
protected void finalize() throws Throwable {
    System.out.println("TreeBean beans count: " + count.decrementAndGet() + " (FINALISATION)");
}


public TreeBean() {  
    super();
    System.out.println("TreeBean beans count: " + count.incrementAndGet() + " (INITIALISATION)");
}  

first.xhtml

  ....
  <h:form id="frm">
        <p:tree
            value="#{treeBean.root}"
            var="node"
            id="tree">
    ....
   <p:commandLink
            action="second.xhtml?faces-redirect=true"
            value="toSecond" />
    ....            

second.xhtml

  ....
  <h:form id="frm">
    ....
   <p:commandLink
            action="first.xhtml?faces-redirect=true"
            value="toFirst" />
    ....

sysout:

  INFO  [stdout] (http--0.0.0.0-8080-4) TreeBean beans count: 1 (INITIALISATION)
  INFO  [stdout] (http--0.0.0.0-8080-4) TreeBean beans count: 2 (INITIALISATION)
  INFO  [stdout] (http--0.0.0.0-8080-4) TreeBean beans count: 3 (INITIALISATION)
  ......
  INFO  [stdout] (Finalizer) TreeBean beans count: 2 (FINALISATION)
  INFO  [stdout] (Finalizer) TreeBean beans count: 1 (FINALISATION)
  INFO  [stdout] (Finalizer) TreeBean beans count: 0 (FINALISATION)

and all thinks came well till I've added dependency to other @ViewScoped bean

TreeBean.java

@ManagedBean
@ViewScoped
public class TreeBean implements Serializable {
 private TreeNode root;  

@ManagedProperty(value = "#{treeNodeBean}")
private TreeNodeBean treeNodeBean;


 public static AtomicInteger count = new AtomicInteger(0);

@Override
protected void finalize() throws Throwable {
    System.out.println("TreeBean beans count: " + count.decrementAndGet() + " (FINALISATION)");
}


public TreeBean() {  
    super();
    System.out.println("TreeBean beans count: " + count.incrementAndGet() + " (INITIALISATION)");
}  

TreeNodeBean.java

@ManagedBean
@ViewScoped
public class TreeNodeBean implements Serializable {

     private String treeNodeItem="TreeNodeItem";

}

And after that no one bean has been released. Does somebody know how to deal with it? Does this a bug or it may be configured somewhere?


回答1:


Unfortunately, you're correct, There are known issues with @ViewScoped memory management(and isn't concerned with just chaining the views) as you'll see here and here. Also look at this question What you could experiment with is to get your hands on the UIViewRoot object per the current session and call getViewMap().remove("myView") based on some event. You could also try this

Unrelated to this, why're you chaining view scoped beans? They're intended to be used as named, for views. Are you constrained from using the SessionScoped?



来源:https://stackoverflow.com/questions/13091843/linked-viewscoped-beans-lead-to-memory-leaks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!