• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.OutOfMemoryError: Java heap space

 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
     
    Sherif Shehab
    Ranch Hand
    Posts: 485
    Android Eclipse IDE Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 13074
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 485
    Android Eclipse IDE Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 60
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

     
    Sherif Shehab
    Ranch Hand
    Posts: 485
    Android Eclipse IDE Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 13074
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 485
    Android Eclipse IDE Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 !!!
    tomcat.JPG
    [Thumbnail for tomcat.JPG]
    the tomcat options
     
    William Brogden
    Author and all-around good cowpoke
    Rancher
    Posts: 13074
    6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 )
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic