Tomcat application gets a java.lang.OutOfMemoryError

Posted on 2006-05-01
Last Modified: 2013-11-24
I've inherited a highly customised and long-in-the-tooth Struts-based application. I ported this in short order from Weblogic 5.1 + Solaris VM Solaris JDK1.2 + Oracle 8 + load balancing (shared server) to Tomcat 5.5 + Sun J2SE JDK1.5 + PostgreSQL + no load balancing (dedicated server with local database), and am still patching bits and pieces. It seems to be functional enough to service the essential business requirements, but the site collapsed this morning with a java.lang.OutOfMemoryError on every page access, which I had to remedy by restarting Tomcat.

Trawling through the logs, I can see java.lang.OutOfMemoryError thrown increasingly when JSPs were compiled (actually during the .java compilation). The pages concerned aren't really a major concern to me, because they are clearly someone or something sniffing around the site looking at pages with obsolete content. I can live with the HTTP/500 errors on those in the short term. However, the frequency of their occurrence increased from the first one and only occurrence on April 28th to today's complete loss of service on 1st May. On 29th-30th we saw them start to take effect for compilation of small JSPs and ultimately we saw java.lang.OutOfMemoryError for every page access the morning regardless of whether it needs to compile or not.


   May 1, 2006 3:25:02 AM org.apache.catalina.core.ApplicationDispatcher invoke
   SEVERE: Servlet.service() for servlet jsp threw exception
   java.lang.OutOfMemoryError: Java heap space

Presumably we are leaving ourselves with less and less available heap for compilation as the application is left to run. I have debug="1" and autoDeploy="true", which reflects the short-order/last-minute nature of this port - or perhaps it just reflects my own naivety! I am using Sun's JDK1.5 compiler and VM rather than Tomcat's recommended Eclipse JDT compiler, because I am using generics.

Tomcat is running with the VM's default 64M, which presumably ought to be increased (the server is a 1GB Dual Xeon).

Here's the start-up:

/usr/java/jdk1.5.0_06/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/usr/java/apache-tomcat-5.5.15/conf/ -Djava.endorsed.dirs=/usr/java/apache-tomcat-5.5.15/common/endorsed -classpath :/usr/java/apache-tomcat-5.5.15/bin/bootstrap.jar:/usr/java/apache-tomcat-5.5.15/bin/commons-logging-api.jar -Dcatalina.base=/usr/java/apache-tomcat-5.5.15 -Dcatalina.home=/usr/java/apache-tomcat-5.5.15 org.apache.catalina.startup.Bootstrap start

Bearing in mind that I need this site to be stable over the next 7 days, what's the most sensible approach to work around my memory leak? Increase heap? Automatically bounce the site in the event we see a java.lang.OutOfMemoryError??? Any suggestions, gentlefolk?
Question by:rstaveley
    LVL 23

    Assisted Solution


    There must be an instant loop, which creates big objects, You should check Your code, or Your application is so heavy,
    that it uses the memory JVM is allowed to use. To increase the amount of JVM memory,
    pass an argument to java an argument:

    java -Xmx256M ....... or 512 based on heapsize

    This sets the ammount of memory to 256MB, while by default java uses 64MB

    LVL 17

    Author Comment

    Thanks for the swift response, rama_krishna580. With only 1GB on the server, I've increased it now to a conservative -Xmx128m and will increase it again if I get bitten again. Given the timescale I'm only realistically looking for a work-around rather than a solution. It is hard to believe that it is anything fast-acting bearing in mind that the loss of heap has appeared to be quite steady since Friday. I expect that there are some persistent caches get populated when the obsolete bits of the site are accessed.

    Do you know of any monitoring tools that I could graph freeMemory(), totalMemory() and maxMemory() from Runtime.getRuntime() to get a picture of the extent of the problem?
    LVL 11

    Accepted Solution

    128m for a 1GB machine is still low. In any decent production env. you should atleast keep it to 512m, so that even if there is any memory leaks, one can still be productive.

    Eclipse has a plugin called Eclipse Profiler at
    LVL 23

    Assisted Solution

    LVL 30

    Assisted Solution

    LVL 17

    Author Comment

    There are some great suggestions here. Bearing in mind that I'm increasing from the default 64M, I hope that I've got some leeway before I start needing the likes of -XX:+AggressiveHeap, but I'll certainly bear it in mind.

    fargo/mayankeagle, thanks for putting me onto profilers. I'll have a go with because that seems to be limited to the specific area of my concern and therefore should hopefully be quicker to get up to speed with. The Eclipse plugin looks wonderful, but I think there'd be more familiarisation required, because I'm not an Eclipse user.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now