在 JBoss 7.1.1 上的 JavaEE6 项目(EJB3、JSF2)中,@ViewScoped bean 似乎存在内存泄漏。上树的日子我花时间研究这个问题。所以我创建了一个带有两个页面的简单项目,以保证在离开@ViewScoped bean 的第一页之后将被释放。
<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>
树豆.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)");
}
第一个.xhtml
....
<h:form id="frm">
<p:tree
value="#{treeBean.root}"
var="node"
id="tree">
....
<p:commandLink
action="second.xhtml?faces-redirect=true"
value="toSecond" />
....
第二个.xhtml
....
<h:form id="frm">
....
<p:commandLink
action="first.xhtml?faces-redirect=true"
value="toFirst" />
....
系统输出:
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)
在我向其他 @ViewScoped bean 添加依赖项之前,所有的想法都很好
树豆.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)");
}
树节点Bean.java
@ManagedBean
@ViewScoped
public class TreeNodeBean implements Serializable {
private String treeNodeItem="TreeNodeItem";
}
在那之后,没有一颗豆子被释放出来。有人知道如何处理吗?这是一个错误还是可能在某处配置?