2011-09-17 21 views
0

我們有一個運行在glassfish服務器上的網頁。在我們的jsp文件中,我們有很多包含如下所示的內容。在jsp文件中包含大量jspf文件導致高內存使用率

<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子句和更多的內存使用增長。任何想法,我可以優化代碼,或者我可以如何更改servlet配置來降低內存使用率?

回答

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. 

source

0

您正在使用JSF。在創建視圖時,如果if語句的計算結果爲true,則頁面上的JSF控件將添加到組件樹中。對於服務器端狀態保存,這些UIComponent實例和their state將(默認情況下)保留在用戶的會話中。您添加的控件越多,您要消耗的內存就越多。默認情況下,會話中會保留一些舊視圖。

你可以嘗試:

  • 沒有建設大型對象圖
  • 減少了會議的若干意見(見com.sun.faces.numberOfViewsInSessioncom.sun.faces.numberOfLogicalViews或等效爲您實現初始化參數)
  • 使用JSF版本與partial state saving如果你還沒有(這可能涉及到升級Glassfish)
  • 實施StateManager保存你的狀態從RAM(例如數據庫,但是這會導致其自身的問題),或者切換到客戶端狀態的默認實現節能(見javax.faces.STATE_SAVING_METHOD - 這個帶有安全問題,並可能改變應用程序的行爲)
相關問題