Solved

JSP doesn't release the memory used

Posted on 2006-11-16
15
594 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:rospcc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +2
15 Comments
 
LVL 2

Assisted Solution

by:Mr_Lenehan
Mr_Lenehan earned 100 total points
ID: 17954686
I think this is a Tomcat problem, unfortunately I'm not a Tomcat user (I use Resin).  Somewhere in your Tomcat configuration the server will have a number of sessions that it keeps alive, and a time period for each session.  

It sounds to me like Tomcat is either:
A) Keeping your sessions alive for too long (when the client has disconnected)
B) Allowing too many sessions (as oppose to freeing up memory from the old sessions)

Hope this helps.
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17954779
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.
0
 
LVL 5

Assisted Solution

by:deeppra
deeppra earned 100 total points
ID: 17954803
Below u have all the Tomcat server config
http://tomcat.apache.org/tomcat-4.1-doc/config/manager.html

How you simulate the load use a standalone application to create many threads and connect to the server

Otherwise u can use JMeter for seeing the performance
here is the link to download JMeter
http://jakarta.apache.org/site/downloads/downloads_jmeter.cgi

using this application u can test the performance of u r web application. Its an opensource and its very much userfriendly

you can find all the info & tutorials about JMeter
http://jakarta.apache.org/jmeter/
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:deeppra
ID: 17954808
dont store too many values in the session this will cause outofMemory problem.
0
 
LVL 29

Accepted Solution

by:
bloodredsun earned 300 total points
ID: 17955340
Many of the points raised here are good.

As for the memory issue, as mentioned it's likely to be the session use. Ways to deal with this are to reduce the length of session use by configuring the value in the web.xml (specified in minutes). The default is 30 minutes (as you can tell from the Task Manager) but generally 6 minutes is easily long enough. (In Tomcat the location of this element is important so check the dtd/schema)

    <session-config>
          <session-timeout>6</session-timeout>
    </session-config>

For pages that do not need sessions, you should include this at the top of the page which will prevent unnecessary session creation

<%@ page session="false" %>

For a production system, I would not recommend using Tomcat as a standalone web server as it cannot serve static content fast enough. The best solution would be to us Apache in front of Tomcat and join them together using mod_jk. This allows Apache to serve all the static content and Tomcat to serve all the dynamic content such as JSP and Servlets. Another option would be to use a higher performing JSP/Servlet container such as Resin.
0
 

Author Comment

by:rospcc
ID: 17993456
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.
0
 
LVL 12

Expert Comment

by:enachemc
ID: 17993754
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.
0
 

Author Comment

by:rospcc
ID: 18001326
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?
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 18001765
Can you please post your code?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18001777
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).
0
 

Author Comment

by:rospcc
ID: 18001795
<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>
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 18001825
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">
...
..
.
0
 

Author Comment

by:rospcc
ID: 18001869
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?
0
 
LVL 5

Expert Comment

by:deeppra
ID: 18001969
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
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Article by: Justin
In light of the WannaCry ransomware attack that affected millions of Windows machines, you might wonder if your Mac needs protecting. Yes, it does and here is how to do it.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question