JSF2 @ViewScoped bean永远不会被破坏

时间:2012-12-20 17:27:41

标签: jsf-2 memory-leaks mojarra

我已经花了3天时间搜索可能的解决方法,因此我的ViewScoped bean不会导致OutOfMemoryException,但没有任何运气。

首先我的环境: JBoss AS 7.1.1.final with Mojarra 2.1.7

我想,ViewScoped bean会在会话到期时被销毁,但它们不会被销毁(使用堆转储检查)。我找到了Mojarra 2.1.16的以下新功能,它修复了这个问题,但遗憾的是升级到这个版本目前不是一个选项: http://java.net/jira/browse/JAVASERVERFACES-2561

此问题还与以下线程有关: Destroying view-scoped beans when session ends

当会话结束(注销或会话过期)时,我有什么办法可以删除所有已创建的ViewScoped bean吗?在SessionScoped bean中保存viewMap并调用clear()也不会破坏那些bean。

提前致谢,

丹尼尔

1 个答案:

答案 0 :(得分:0)

为了确定这个错误,我已将我的代码删除到以下部分:

一个非常简单的JSF2页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <f:facet name="first">
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Simple OOM Check</title>
        </f:facet>
    </h:head>
    <h:body>
        <h:outputText value="#{oomCheckBean.testString}"/>
    </h:body>
</html>

使用过的@ViewScoped bean oomCheckBean:

@ViewScoped
@ManagedBean(name = "oomCheckBean")
public class OomCheckBean implements Serializable {
    private static final long serialVersionUID = 6325712415478215045L;

    private String testString;

    @PostConstruct
    public void init() {
        testString = "Hello World";
        System.out.println("I will survive ....");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("... klaaatsch!");
    }

    /**
     * @return the testString
     */
    public String getTestString() {
        return testString;
    }

    /**
     * @param testString the testString to set
     */
    public void setTestString(String testString) {
        this.testString = testString;
    }
}

在此之后我将JMeter测试用例更改为登录,请调用此oomCheck.xhtml页面并注销。在一段时间后我停止了测试执行,并使用JMX首先手动调用垃圾收集器( java.lang.Memory - &gt; Operations - &gt; gc())。在此之后我调用方法来创建堆转储( com.sun.management.HotSpotDiagnostic - &gt; Operations - &gt; dumpHeap())。

结果就像我在我的问题中提到的,内存中有很多OomCheckBean对象。所有者对象为org/jboss/as/web/deployment/ConcurrentReferenceHashMap$HashEntry。任何帮助解决这个问题的人都表示赞赏。就像我上面提到的,如果JSF版本升级到2.1.16,那么这些@ViewScoped bean将被销毁,留下其他的#34;错误&#34;喜欢缺少doctype和AJAX问题。所以如果有人知道如何销毁@ViewScoped bean,当会话变得无效时,我会很高兴。

相关问题