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?
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
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With