Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSF内存泄漏:在同一页面上刷新/重定向时未销毁bean_Jsf_Redirect_Memory Leaks_Managed Bean_View Scope - Fatal编程技术网

JSF内存泄漏:在同一页面上刷新/重定向时未销毁bean

JSF内存泄漏:在同一页面上刷新/重定向时未销毁bean,jsf,redirect,memory-leaks,managed-bean,view-scope,Jsf,Redirect,Memory Leaks,Managed Bean,View Scope,我在managedbean中使用JSF viewscope。 我在html视图中调用托管bean 刷新页面时,允许有一个新的托管bean,而旧的bean保留在内存中。 如果我刷新,刷新,刷新。。。内存中有大量托管bean。 我试着运行垃圾收集器,没有任何附加。 托管bean将一直保留到会话过期。在会话结束时,通过用户操作或超时,托管bean被释放 只有当我停留在同一个页面上时(在浏览器上刷新-F5-或没有JSP标记或java代码重定向的重定向链接),我才会遇到这个问题。 当我更改页面时,只有一个

我在managedbean中使用JSF viewscope。 我在html视图中调用托管bean

刷新页面时,允许有一个新的托管bean,而旧的bean保留在内存中。 如果我刷新,刷新,刷新。。。内存中有大量托管bean。
我试着运行垃圾收集器,没有任何附加。 托管bean将一直保留到会话过期。在会话结束时,通过用户操作或超时,托管bean被释放

只有当我停留在同一个页面上时(在浏览器上刷新-F5-或没有JSP标记或java代码重定向的重定向链接),我才会遇到这个问题。 当我更改页面时,只有一个托管bean被释放(最后一个)。其他的留在记忆中。 它似乎与JSP标记一起工作,托管bean在重定向后被释放

第二个错误: 有一段时间,我遇到了一个奇怪的情况: 我有一些未在内存中释放的托管bean,我使用JSP标记重定向(可能3/4次),系统执行Predestroy/PostConstruct/Predestroy。。。 之后,每个重定向都会对非JSP标记或java重定向执行PostConstruct/Predestroy,对JSP标记执行Predestroy/PostConstruct/Predestroy。 在内存中,未释放的托管bean在此之后被释放。 我真的不知道什么是真的,但它会重复多次

有人能解决这个问题吗? 我看到一个帖子看起来有问题,我报告了一个错误

配置: Java EE 7/glassfish 4

AViewscope.java:

package com.btm.viewscopetest;

import java.io.IOException;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

@ManagedBean
@ViewScoped
public class AViewscope implements Serializable {

    @PostConstruct
    public void postConstruct() {
        System.out.println("PostConstruct");
    }

    @PreDestroy
    public void preDestroy() {
        System.out.println("PreDestroy");
    }

    public AViewscope() {
    }

    public String getSomething() {
        return "Something";
    }

    public void redirect() throws IOException {
        FacesContext facesContext = FacesContext.getCurrentInstance();

        ExternalContext externalContext = facesContext.getExternalContext();

        externalContext.redirect("apage.xhtml");
    }
}
apage.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <head>
        <title>A page</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <h:form>
            <h:outputText value="Get something: #{aViewscope.something}" />

            <br/>
            <br/>

            <h:commandButton value="Stay on this page with java " actionListener="#{aViewscope.redirect}"/>
            <br/>
            <h:commandButton value="Stay on this page with direct link" action="apage" />
            <br/>
            <h:commandButton value="Go to another page" action="anotherPage"/>
            <br/>
            <h:commandLink value="Stay on this page with direct link h tag " action="apage.xhtml" />
            <a href="apage.xhtml">Stay on this page with direct link a tag</a>
        </h:form>

    </body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <head>
        <title>Another Page</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <h:form>
            <h:commandButton value="Go to another page" action="apage"/>
        </h:form>

    </body>
</html>

一页





anotherPage.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <head>
        <title>A page</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <h:form>
            <h:outputText value="Get something: #{aViewscope.something}" />

            <br/>
            <br/>

            <h:commandButton value="Stay on this page with java " actionListener="#{aViewscope.redirect}"/>
            <br/>
            <h:commandButton value="Stay on this page with direct link" action="apage" />
            <br/>
            <h:commandButton value="Go to another page" action="anotherPage"/>
            <br/>
            <h:commandLink value="Stay on this page with direct link h tag " action="apage.xhtml" />
            <a href="apage.xhtml">Stay on this page with direct link a tag</a>
        </h:form>

    </body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <head>
        <title>Another Page</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>
    <body>
        <h:form>
            <h:commandButton value="Go to another page" action="apage"/>
        </h:form>

    </body>
</html>

另一页
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

javax.faces.PROJECT_阶段
发展
Facesservlet
javax.faces.webapp.FacesServlet
1.
Facesservlet
*.xhtml
30
index.xhtml
我在内存中有大量的托管bean。 我试着运行垃圾收集器,没有任何附加

这不是一个bug,它是故意的。如果
web.xml
中的
javax.faces.STATE\u SAVING\u方法设置为
server
,JSF会在内存中维护一定数量的bean。客户端现在将始终提交一个
javax.faces.ViewState
(id),允许服务器识别要加载的适当bean

通过将上下文参数com.sun.faces.numberOfViewsInSession
设置为符合您需要的数字,可以限制每个会话存储的视图数量

但是要注意,如果您选择
1
,并且用户使用向后导航,那么如果bean被删除,他将遇到
ViewExpired
异常


另一种减少内存负载的方法是通过将
javax.faces.STATE\u SAVING\u METHOD
设置为
client
,将视图存储转移到客户端。但是,这将导致网络流量增加,因为现在客户端不仅需要使用
javax.faces.ViewState
来跟踪,还需要跟踪整个视图本身。我不能百分之百确定客户端选项到底是如何工作的,但对我来说,让客户端跟踪“视图”而不仅仅是标识符是很危险的。

Duplicate:(还有更多类似的帖子,请搜索它们并引用它们。我已经看到了那篇帖子(我在问题中给出了引用)。这并不是真的告诉我如何纠正我的问题…遗憾的是,我不能在我的项目中使用omnifaces。抱歉,因为之前的所有文本,我记得这个链接后就停止阅读。下次最好在问题中早点发布这些。好的。你有什么想法来解决吗?嗯,有更多的帖子。一些相关的观点在那么,也许它们没有像回答中所说的那样被释放,你如何判断“这是故意的”?(不是讽刺,这是一个真正的问题)如果是故意的,为什么我使用JSP标记时,托管bean被释放?为什么当我处于正常导航(从一个页面到另一个页面)时会这样做,托管bean是免费的?我理解重用托管的原则,即不浪费单独内存(池内存)的时间,但对我来说我们不是这样。我尝试了
javax.faces.STATE\u SAVING\u方法
com.sun.faces.numberOfViewsInSession
,我尝试并重试了这个不起作用的方法。你尝试了吗?