CDI会话范围bean未被破坏导致内存泄漏

时间:2015-05-21 20:53:20

标签: jsf cdi destroy weld session-scope

我对会话作用域CDI bean的生命周期有疑问 据我所知,会话范围内的CDI bean是在会话开始时由容器构造的,并在会话结束时销毁。在销毁bean之前,将按照https://docs.oracle.com/javaee/6/tutorial/doc/gmgkd.html中的描述调用 @PreDestroy 方法。它还说要在这种方法中释放资源。

在我构建的JSF应用程序中,我遇到内存泄漏,因为bean似乎没有被破坏,因此不会调用 @PreDestroy 方法来释放垃圾收集器的一些引用。所以我构建了一个简单的应用程序来测试行为。我的经验是会话bean在会话结束时不会被破坏,而且当需要内存空间时它甚至不会被破坏。我不敢相信我是第一个遇到这种情况的人,但我找不到有关此行为的任何信息..

所以我的问题是:不应该销毁CDI bean - 因此调用 @PreDestroy 方法 - 在其上下文过期后立即调用?如果不是,那么当需要空间时,它至少不会被破坏?

我的测试应用程序:

我不允许发布图片,但大纲是eclipse生成的非常基本的jsf webapp。我也有beans.xml文件。

Test.java:

package com.test;

import java.io.Serializable;
import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@SessionScoped
@Named
public class Test implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String test;
    private ArrayList<ComplexType> cps;
    private ArrayList<ComplexType> cps_2;

    @PostConstruct
    public void init() {
        System.out.println("test postconstruct..");
        test = "Cdi Test";
    }

    @PreDestroy
    public void cleanUp() {
        cps = null;
        cps_2 = null;
        System.out.println("test cleanUp....");
    }

    public void data_1() {

        cps = new ArrayList<ComplexType>();

        for(int i = 0; i < 800; i++) {
            String[] s = new String[100000];
            ComplexType cp = new ComplexType(i, s);
            cps.add(cp);
            System.out.println(i);
        }
        System.out.println("data_1");
    }

    public void free_1() {
        cps = null;
        System.out.println("free_1");
    }

    public void data_2() {

        cps_2 = new ArrayList<ComplexType>();

        for(int i = 0; i < 800; i++) {
            String[] s = new String[100000];
            ComplexType cp = new ComplexType(i, s);
            cps_2.add(cp);
            System.out.println(i);
        }
        System.out.println("data_1");
    }

    public void free_2() {
        cps_2 = null;
        System.out.println("free_1");
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }   
}

ComplexType.java:

package com.test;

public class ComplexType {

    private int id;
    private String[] name;

    public ComplexType(int id, String[] name) {

        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String[] getName() {
        return name;
    }
    public void setName(String[] name) {
        this.name = name;
    }
}

的index.xhtml:

<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>
    <title>Cdi test </title>
</h:head>

<h:body>

    <h:outputText value="#{test.test}"></h:outputText>

    <h:form>
        <h:commandButton value="cp_1 data" actionListener="#{test.data_1}">
            <f:ajax></f:ajax>
        </h:commandButton>
        <h:commandButton value="cp_1 Free" actionListener="#{test.free_1}">
            <f:ajax></f:ajax>
        </h:commandButton>

        <br></br>
        <h:commandButton value="cp_2 data" actionListener="#{test.data_2}">
            <f:ajax></f:ajax>
        </h:commandButton>
        <h:commandButton value="cp_2 Free" actionListener="#{test.free_2}">
            <f:ajax></f:ajax>
        </h:commandButton>
    </h:form>

</h:body>
</html>

我打开index.xhtml页面,按预期调用 @PostConstruct 方法。当我调用data_1和data_2时两者都没有释放,超出了堆空间。当我释放其中一个资源或者我连续两次调用一个方法时,堆空间就足够了,因为垃圾收集器会释放内存。这可以正常运作。

但是当我调用一个数据函数时,关闭浏览器,然后关闭会话,打开一个新浏览器并再次调用其中一个数据函数,然后应用程序停止工作(我猜)超出了内存空间即可。关键是:第一个会话bean没有被销毁,而且 @PreDestroy 方法没有被调用,因此ArrayList仍然在内存中。

有人可以向我解释这里发生了什么吗?如果CDI bean的上下文过期,它就不应该被容器销毁,以便引用可以设置为null并且垃圾收集器可以释放资源吗?
我正在使用JBoss AS 7.1.1及其默认实现JSF Mojarra 2.1。

2 个答案:

答案 0 :(得分:6)

会话bean(无论CDI或JSF托管)保持活动状态,直到某个会话超时(通常默认为30分钟,取决于应用程序服务器),您可以在web.xml中指定。关闭浏览器不会使会话无效,等待超时到期后servlet容器将其销毁。所以,我的假设,这种行为很好,@ Peretroy方法将在以后调用。

答案 1 :(得分:2)

@olexd的答案基本上解释了我脑子里的错误,非常感谢你!但是在一段确定的时间后无效的会话无效,所以我不得不使用@ geert3的评论,谢谢你!我正在回答我自己的问题,以显示我在这里详细解决了我的特定问题。

我错了:我认为会话一旦浏览器关闭就会过期。这是错误的,这是有道理的。有人可能想关闭浏览器并再次打开它,以便像以前一样在同一个会话中工作 对我来说这种行为是不合适的,因为我想在浏览器关闭后立即释放资源。所以答案是手动使会话无效,如下所示:

FacesContext.getCurrentInstance().getExternalContext().invalidateSession();

一旦调用此方法,就会调用 @PreDestroy 方法,完全按照我的意愿调用。现在我必须确定何时调用此函数。我搜索了一种方法来收听类似 browserclose 事件的内容。有 onbeforeunload onunload 事件。 onunload 在Chrome中似乎不适合我,但 onbeforeunload 可以。另见这个答案: https://stackoverflow.com/a/16677225/1566562

所以我编写了一个隐藏按钮,在 beforeunload 上被javascript点击并调用适当的backingbean方法。这是我希望它工作的工作原理。我在Chrome 43.0.2357.65和IE 11上进行了测试,现在我很满意。但它不能与 onunload 一起使用,但我现在不关心这个问题。

所以我的最终代码喜欢这个:

的index.xhtml

<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>
    <title>Cdi test</title>
    <h:outputScript library="default" name="js/jquery-1.11.3.min.js"
        target="head"></h:outputScript>
</h:head>

<h:body>

    <h:outputText value="#{test.test}"></h:outputText>

    <h:form id="overall">
        <h:commandButton value="cp_1 data" actionListener="#{test.data_1}">
            <f:ajax></f:ajax>
        </h:commandButton>
        <h:commandButton value="cp_1 Free" actionListener="#{test.free_1}">
            <f:ajax></f:ajax>
        </h:commandButton>

        <br></br>
        <h:commandButton value="cp_2 data" actionListener="#{test.data_2}">
            <f:ajax></f:ajax>
        </h:commandButton>
        <h:commandButton value="cp_2 Free" actionListener="#{test.free_2}">
            <f:ajax></f:ajax>
        </h:commandButton>

        <br></br>

        <h:commandButton id="b" style="display:none"
            actionListener="#{test.invalidate}"></h:commandButton>

    </h:form>

    <script type="text/javascript">
        $(window).on('beforeunload', function() {
            $('#overall\\:b').click();
        });
    </script>
</h:body>
</html>

Test.java

package com.test;

import java.io.Serializable;
import java.util.ArrayList;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

@SessionScoped
@Named
public class Test implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String test;
    private ArrayList<ComplexType> cps;
    private ArrayList<ComplexType> cps_2;

    @PostConstruct
    public void init() {
        System.out.println("test postconstruct..");
        test = "Cdi Test";
    }

    @PreDestroy
    public void cleanUp() {
        cps = null;
        cps_2 = null;
        System.out.println("test cleanUp....");
    }

    public void data_1() {

        cps = new ArrayList<ComplexType>();

        for (int i = 0; i < 800; i++) {
            String[] s = new String[100000];
            ComplexType cp = new ComplexType(i, s);
            cps.add(cp);
            System.out.println(i);
        }
        System.out.println("data_1");
    }

    public void free_1() {
        cps = null;
        System.out.println("free_1");
    }

    public void data_2() {

        cps_2 = new ArrayList<ComplexType>();

        for (int i = 0; i < 800; i++) {
            String[] s = new String[100000];
            ComplexType cp = new ComplexType(i, s);
            cps_2.add(cp);
            System.out.println(i);
        }
        System.out.println("data_2");
    }

    public void free_2() {
        cps_2 = null;
        System.out.println("free_2");
    }

    public void invalidate() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        System.out.println("invalidate");
    }

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }

}

请注意,我使用过JQuery。这适用于JBoss AS 7.1.1和默认的Weld实现 要添加的一件事:一个不必手动将所有引用设置为null。这也是有道理的,因为它会很乏味......

相关问题