在jsp文件中包含大量jspf文件会导致内存使用率高

在jsp文件中包含大量jspf文件会导致内存使用率高,jsp,glassfish,memory-management,jspinclude,jsp-fragments,Jsp,Glassfish,Memory Management,Jspinclude,Jsp Fragments,我们有一个网页,运行在glassfish服务器上。在我们的jsp文件中,我们有很多类似下面的包含 <jsp:directive.include file="johndoe/foobar.jspf"/> 这些文件根据用户的选择包括在内。我们的jsp文件基本上如下所示: <jsp:root version="2.1" xmlns:c="http://java.sun.com/jstl/core_rt" xmlns:f="http://java.sun.com/jsf/core

我们有一个网页,运行在glassfish服务器上。在我们的jsp文件中,我们有很多类似下面的包含

<jsp:directive.include file="johndoe/foobar.jspf"/>

这些文件根据用户的选择包括在内。我们的jsp文件基本上如下所示:

<jsp:root version="2.1" xmlns:c="http://java.sun.com/jstl/core_rt"
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:jsp="http://java.sun.com/JSP/Page" 
xmlns:webuijsf="http://www.sun.com/webui/webuijsf">
//some unimportant codes here
<c:if test="${sessionScope.loadAvgTest}">
  //some unimportant codes here
  <f:subview id="reports15">
    <jsp:directive.include file="siforms/sysinfoform.jspf"/>
  </f:subview>
  //some unimportant codes here                                        
  <f:subview id="reports16">
    <jsp:directive.include file="siforms/sysinfoformscheduled.jspf"/>
  </f:subview>
   //some unimportant codes here
</c:if>
//some unimportant codes here
<c:if test="${sessionScope.bandwidthTest}">
  //some unimportant codes here
  <f:subview id="reports17">
    <jsp:directive.include file="mailforms/mailfilter.jspf"/>
  </f:subview>
  //some unimportant codes here
  <f:subview id="reports18">
    <jsp:directive.include file="mailforms/mailfilterscheduled.jspf"/>
  </f:subview>
//some unimportant codes here
</c:if>
....

//这里有些不重要的代码
//这里有些不重要的代码
//这里有些不重要的代码
//这里有些不重要的代码
//这里有些不重要的代码
//这里有些不重要的代码
//这里有些不重要的代码
//这里有些不重要的代码
....

大约有80个像这样的if语句,每个语句包含2个inculde。当我删除了很多if子句,只留下了一些if和一些include语句时,内存使用情况很好。但是随着我使用更多的if子句和更多的include,内存使用量也在增长。您知道如何优化代码,或者如何对servlet配置进行配置更改以降低内存使用率吗?

您正在使用JSF。在视图创建时,如果
if
语句的计算结果为true,则页面上的JSF控件将添加到组件树中。对于服务器端状态保存,这些实例和将(默认情况下)保留在用户会话中。添加的控件越多,消耗的内存就越多。默认情况下,会话中将保留许多旧视图

你可以试试:

  • 不构建大型对象图
  • 减少会话中的视图数(请参见
    com.sun.faces.numberOfViewsInSession
    com.sun.faces.numberOfLogicalViews
    或实现的等效初始化参数)
  • 使用JSF版本,如果您还没有(这可能涉及升级Glassfish)
  • 实现从RAM中保存状态(例如,保存到数据库,但这会导致其自身的问题)或使用默认实现切换到客户端状态保存(请参阅
    javax.faces.state\u saving\u方法
    -这会带来安全问题,并可能会改变应用程序的行为)

使用
jsp:include
而不是
jsp:directive.include
解决了这个问题。 据我的研究所知,
jsp:directive.include
(include directive)在编译时将文件包含到jsp页面中,而
jsp:include
在运行时包含输出

I have found that, include action (runtime include) runs a bit slower 
yet it is preferred generally because it save a lot of memory of the system. 
()