Link to home
Start Free TrialLog in
Avatar of rospcc
rospcc

asked on

JSP doesn't release the memory used

Hi All,

I tried to simulate load on a pure simple JSP page. After tens of thousands hits, it actually gives me Out.of.Memory error. Is this because I code in JSP and using Tomcat as the server? Would it be better if I code in Servlet?
Furthermore, when I went to the Task Manager, I saw that the memory used by java.exe (Tomcat) stays high for 30 minutes.

Please share with me if you have had any experience that could work around this, thanks.
SOLUTION
Avatar of Mr_Lenehan
Mr_Lenehan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is no difference between JSP and Servlet. The server compiles the JSP into a servlet (you can find out the source of the generated servlet in the server). You should not store data in your servlet and you should avoid storing large data into the session. If you want to pass parameters, you're better using the request attributes rather than the session, because the request attributes are only valid for one request.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of deeppra
deeppra

dont store too many values in the session this will cause outofMemory problem.
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rospcc

ASKER

Thanks for all your responses.  Actually, I tried with a pure simple JSP page which contains no session at all. In fact there's only one jsp code: System.out.println. But I still get the out.of.memory error.

Does java has any way to free up the memory being used? Especially since the test that I've conducted has no session and no database connection, but the memory still shot up and stays there for 30mins. How do we make the memory to be released once the page had been delivered from the server to clients? Is calling the Garbage Collector "System.gc()" the only way?

We might not be able to set session-timeout to 6 min, Bloodredsun. Our application involves a survey questionnaire where users'll need some time to think and write their comments. If the session-timeout is 6 min, they might be logged out everytime they've finished inputting comments and proceed to next question.
I'm thinking of trying Resin to see whether it will help to control the memory usage.
you should not call yourself "System.gc()". This is not a good ideea. The memory will be freed up by the system when needed. It would not solve your problem. The memory leak must be elsewhere.

Can you post your code ?

You could increase the memory used by the java server JVM by using  -Xms128m -Xmx512m (you can change the values). This instructs the Java process to start with 128 MB of RAM and use at maximum 512 MB.
Avatar of rospcc

ASKER

enachemc, there are only 53 lines of codes in that JSP page now. It's practically left only html codes and 1 System.out.println(). Increase the memory by changing to -Xms128m -Xmx512m will definitely prolong the issue. However, I think it won't solve the issue, because I haven't found a way to release the memory used. Because eventhough after I've stopped the simulation load (i.e. load of users accessing the server), the memory used still stay very high (As shown in the Task Manager). Is there any way to release the memory?

Btw, do we need to pay licence if we use Resin server OPL for commercial use?
Can you please post your code?
It means that the problem lies elsewhere in your code (not in the JSP we are talking about). Maybe a bacground thread launched at app startup, maybe multiple redeploys of the same application (this tends to produce a PermGenSpace memory error).
Avatar of rospcc

ASKER

<HTML>

<HEAD>
<TITLE>Application</TITLE>
<base target="_parent">

</HEAD>
<BODY LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
<form name="Top" action="Top.jsp" method="post">


<table width="800" cellpadding="0" cellspacing="0" class="fontstyle" height="133" id="table1">
  <tr>
    <td width="10" bgcolor="#99FF33">&nbsp;</td>
    <td background="images/App_Banner.jpg" valign="bottom" width="790">
            <table width="782" height="138" id="table2">
              <tr>
                  <td background="images/App_Banner.jpg" align= "left" valign="bottom" width="776" height="134">
      
                        <table width="219" id="table3">
                          <tr>
                              <td width="64" align="left"><font color="#FFFFFF"><span class="style1">
                              <font size="3">Login</font></span></font></td>        
                              <td width="92">
                              <input type="text" name="txtUsername" size="8" tabindex = 1 onKeyPress="if(event.keyCode==13) { return validate(); }"></td>
                              <td width="51" rowspan="2" valign="bottom" style="cursor:hand" onclick="return validate()"><font color="#FFFFFF"><b>
                              Go</b></font></td>
                          </tr>
                          <tr>
                              <td align="left"><font color="#FFFFFF"><span class="style1">
                              <font size="3">Password</font></span></font></td>
                              <td>
                              <input type="password" name="txtPassword" size="10" tabindex = 2 onKeyPress="if(event.keyCode==13) { return validate(); }"></td>
                          </tr>
                        </table>
                  </td>
              </tr>
            </table>
            <p>&nbsp;</td>
  </tr>
  <tr>
    <td width="10" bgcolor="#99FF33">&nbsp;</td>
    <td background="images/App_Banner.jpg" valign="bottom" width="790">
            &nbsp;</td>
  </tr>
  <tr>
    <td width="10" bgcolor="#99FF33">&nbsp;</td>
    <td background="images/App_Banner.jpg" valign="bottom" width="790">
            &nbsp;</td>
  </tr>
</table>
<%       //System.gc();
      System.out.println("test"); %>

</BODY>
</HTML>
You are creating a session!

Sessions are created by default when you visit a JSP. If you want to stop sessions being created you need to add in the correct declaration (<%@ page session="false" %>) , as I said earlier!
----------

<%@ page session="false" %>
<HTML>

<HEAD>
<TITLE>Application</TITLE>
<base target="_parent">

</HEAD>
<BODY LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
<form name="Top" action="Top.jsp" method="post">
...
..
.
Avatar of rospcc

ASKER

I did try it (i.e. <%@ page session="false" %>), bloodredsun. It did prolong, but it gets out.of.memory error too eventually.

Actually in my application, I need to create session in this page. The fact that it already dies even without the session. I begin to question whether JSP is using too much memory and doesn't release them fast enough, or perhaps Tomcat may not be the appropriate container. Does anybody know whether we need to pay licence if we use Resin server OPL for commercial use?
but i tried in my system its working fine i simulated using JMeter and after 30000 request also its working fine
if u send u r mail  address i will send u a screenshot