File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes java.lang.OutOfMemoryError: Java heap space Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "java.lang.OutOfMemoryError: Java heap space" Watch "java.lang.OutOfMemoryError: Java heap space" New topic
Author

java.lang.OutOfMemoryError: Java heap space

Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Hi Guys,
i;m getting this error below , it;s j2ee application , getting some data form DB then display it in excel sheet .
my environment :
1- No reporting framework used
2- using apache POI
3- IDE myeclipse
4- tomcat server
5- Windows xp

what does this error mean? and how to fix it


Thanks,
Sherif
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689


Hi,

Check data you get from DB and print the size of that data. If that data contains so many size and you try to store that data into some variable then in general it gives erro like this.



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
1. How much memory are you giving Tomcat?
2. Does this happen on the first request or does it take multiple requests to cause it?

Bill
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

I often see mis-behaving recursive calls triggering this type of error. If you've got any recursive calls in your code, you might want to monitor them.

-Cameron McKenzie
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

You can do the following

  • Check how much memory is allocated to tomcat. Tune it with the -Xmx and -Xms switch
  • If you are loading too many classes, you may be required to increase the perm gen space of the heap
  • Use a profiler to find out where all that memory is being eaten up.


  • If i remember correctly POI has a problem loading more than N number of records into an excel sheet. I cant confirm though. I heard something similar from a colleague a while back, but its so far back that I cannot remember


    SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
    Sherif Shehab
    Ranch Hand

    Joined: Mar 05, 2007
    Posts: 483

    William Brogden wrote:1. How much memory are you giving Tomcat?
    2. Does this happen on the first request or does it take multiple requests to cause it?

    Bill


    Actually , how i know the amount of memory given to the tomcat server ?
    i tried java-Xms265m -Xmx512m but this response occured is this ok ?

    How can i display the current values for Xms and Xmx ?

    Any recommendations Guys ?
    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12835
        
        5
    IF - you are starting Tomcat via the startup.bat /catalina.bat method

    THEN - You can control the amount of memory Tomcat starts with by adding a JAVA_OPTS environment setting. For example to give Tomcat and initial 128mb with maximum 256mb:

    Insert in Catalina.bat around line 43 after the block of REM statements

    set JAVA_OPTS=-Xms128m -Xmx256m

    Look further in catalina.bat to see where JAVA_OPTS is used.

    Bill
    Sherif Shehab
    Ranch Hand

    Joined: Mar 05, 2007
    Posts: 483

    William Brogden wrote:IF - you are starting Tomcat via the startup.bat /catalina.bat method

    THEN - You can control the amount of memory Tomcat starts with by adding a JAVA_OPTS environment setting. For example to give Tomcat and initial 128mb with maximum 256mb:

    Insert in Catalina.bat around line 43 after the block of REM statements

    set JAVA_OPTS=-Xms128m -Xmx256m

    Look further in catalina.bat to see where JAVA_OPTS is used.

    Bill

    What if i;m running tomcat through tomcat6.exe ?
    Mateus Lucio
    Ranch Hand

    Joined: Jul 27, 2006
    Posts: 57
    Hi there!

    I suggest you track down the problem's source ... what is using so much memory that is crashing the jvm?

    if you just give it more memory, without knowing what's causing the problem, sooner or later you'll get it again.


    Studying ...
    Sherif Shehab
    Ranch Hand

    Joined: Mar 05, 2007
    Posts: 483

    Mateus Lucio wrote:Hi there!

    I suggest you track down the problem's source ... what is using so much memory that is crashing the jvm?

    if you just give it more memory, without knowing what's causing the problem, sooner or later you'll get it again.


    Guys ,

    I found this exceptions occurs after retrieving 81920 record from the DB here is my code :



    i reviewed my class all looks fine ,is there a way to maintain this part of code to prevent this exception ?
    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12835
        
        5
    It may not be a coding flaw, your problem is just too big for the default Java memory allocation of 64mb.

    You should either:
    1. use startup.bat with the memory setting JAVA_OPTS line I showed before
    - or -
    2. figure out how to assign more memory when starting using tomcat6.exe

    For 2 - I cant help you there, I always use startup.bat for maximum control of the environment.

    Bill

    Sherif Shehab
    Ranch Hand

    Joined: Mar 05, 2007
    Posts: 483

    William Brogden wrote:It may not be a coding flaw, your problem is just too big for the default Java memory allocation of 64mb.

    You should either:
    1. use startup.bat with the memory setting JAVA_OPTS line I showed before
    - or -
    2. figure out how to assign more memory when starting using tomcat6.exe

    For 2 - I cant help you there, I always use startup.bat for maximum control of the environment.

    Bill


    int the tomcat directory i found tomcat6w.exe in the java tab i can set initial memory pool and maximum memory pool i set them to 256 and 512 , and restarted the server and run my application but still the java heap error occurs !!!



    [Thumbnail for tomcat.JPG]

    William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12835
        
        5

    Did the number 81920 change with more memory allocated?

    If this was my problem I would try inserting logging statements at various points using the Runtime freeMemory() call to find out how much memory is left at various points.

    Bill
    Billy JK
    Greenhorn

    Joined: Oct 02, 2002
    Posts: 20
    How are you doing your data processing ? How do you open your result set ? Any special parameters ?

    If you're basically just dumping data from the DB to Excel, it's your treatment that loads too much data in memory, even if you increase the Tomcat memory to the max available, it might still crash.

    Your code looks rather like you're constructing an object in memory that will hold data for all your available rows. Can't you process one line at a time and write to excel after each processed line ? ( but I might have gotten wrong your code excerpt, please add details if this is the case )
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: java.lang.OutOfMemoryError: Java heap space