问题描述
在JBoss 7.1.1上的JavaEE6项目(EJB3,JSF2)中,似乎我们的@ViewScoped bean发生了内存泄漏.在最后的树上,我花了一些时间进行此问题的调查.因此,我创建了一个包含两个页面的简单项目,以保证第一页离开@ViewScoped bean之后将被释放.
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
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
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
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)
在我将依赖项添加到其他@ViewScoped bean之前,所有想法都很好
and all thinks came well till I've added dependency to other @ViewScoped bean
TreeBean.java
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
TreeNodeBean.java
@ManagedBean
@ViewScoped
public class TreeNodeBean implements Serializable {
private String treeNodeItem="TreeNodeItem";
}
在那之后,没有一个bean被释放.有人知道如何处理吗?这是bug还是可能在某处配置?
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?
推荐答案
不幸的是,您是正确的,因为@ViewScoped
内存管理存在一些已知问题(并且与仅链接视图无关)请参见此处和此处.还要查看这个问题您可以尝试的方法是,在当前会话中尝试使用UIViewRoot
对象,并且根据某些事件调用getViewMap().remove("myView")
.您也可以尝试此
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
与此无关,为什么要链接视图范围的bean?它们旨在用作视图的命名.您是否无法使用SessionScoped
?
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
?
这篇关于链接的ViewScoped bean导致内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!