0

我们有一个网页,在 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 个 inculdes。当我删除了很多这些 if 子句并只留下了一些 if 和一些 include 内存使用情况时很好。但是随着我使用更多的 if 子句和更多的内容,内存使用量会增加。有什么想法可以优化代码或如何对 servlet 配置进行配置更改以降低内存使用量?

4

2 回答 2

1

使用jsp:include而不是jsp:directive.include解决问题。据我从研究中了解到的jsp:directive.include(包括指令)在编译时将文件包含到 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. 

来源

于 2011-09-19T13:11:32.897 回答
0

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

你可以试试:

  • 不构建大型对象图
  • 减少会话中的视图数量(请参阅com.sun.faces.numberOfViewsInSessioncom.sun.faces.numberOfLogicalViews/或等效初始化参数以用于您的实现)
  • 如果您还没有使用带有部分状态保存的 JSF 版本(这可能涉及升级 Glassfish)
  • 实现StateManager以将您的状态保存到 RAM 之外(例如,保存到数据库,但这会导致其自身的问题)或使用默认实现切换到客户端状态保存(请参阅javax.faces.STATE_SAVING_METHOD- 这会带来安全问题并可能会改变应用程序行为)
于 2011-09-17T08:59:57.747 回答