"Exception in thread "main" java.lang.OutOfMemoryError: Java heap space"

- Why does this occur?
- practical scenarios seen.
- Can this be handled programatically?
- Should this be handled programatically?

Why does this Occur?
  1. jvm memory leak, ill heap management.
  2. unavailability of sufficient memory for application to load.
  3. unintentional memory retention.

Practical Scenarios:
  1. build failed due to out of memory
  2. unable to load the application
  3. taking huge turn around time by application


Solution:
  1. set ANT_OPTS environment variable.
  2. set new initial size onSun JVM use the -XX:PermSize=64m option when starting the virtual machine and set the maximum permanent generation size use -XX:MaxPermSize=128m option.
  3. increase the RAM of the system.
  4. using caching techniques.
  5. releasing the resources(db or other instance variables) in finally block.

Should this be handled programatically?
- this is where most people would not stand together as heap management is done internally @ JVM level.

Can this be handled programatically? 

- During one of the business requirement, where I was expected to catch OOME & help the application not terminate abruptly.
java.lang.Object  
      |  
     +----java.lang.Throwable  
                  |  
                 +----java.lang.Error  
                             |  
                            +----java.lang.VirtualMachineError  
                                         |  
                                        +----java.lang.OutOfMemoryError


java.lang.Object  
      |  
     +----java.lang.Throwable  
                  |  
                 +----java.lang.Error  
                             |  
                            +----java.lang.ThreadDeath

ThreadDeath is not caught, as catching it means that threads never exit properly.
catch (Throwable ex){
if (!(ex instanceof ThreadDeath))
{ex.printStackTrace(System.err);
}}

read more on: 
  1. ThreadDeath
  2. Java Performance Tuning
  3. Javaspecialists.eu
  4. stackflowover
  5. freshblurbs

 


Comments

Amiet
09/24/2009 00:14

OK, so I have experienced this many times. You have a thread, and during the execution of thread, some exception has been raised.

Now, we all are aware of run() cannot throw any exception. Whatever exception occurred during execution of thread, it will be handled by JVM (unless you use try/catch in your run method).

This exception, OutOfMemoryException, for web applications can be raised because of

1. Memory leak in your code
2. If several (un)necessary threads created in your application
3. Web/App server is under huge load and memory allotted to it is not sufficient.

I’m afraid if you can catch such exceptions in your code. But still we can investigate more on this and come up with some sort of solution.

Good article, keep it up!

Reply



Leave a Reply

    Categories

    All
    5800
    Business Card
    Cardly
    Collaboration Tool
    Fileupload
    Google Wave
    Gtalk
    Java
    Mvc
    Nokia
    Oome
    Servletcentric
    Shortcut
    Struts2
    System
    Website
    Windows
    Zoho

    Archives

    December 2009
    November 2009
    September 2009
    August 2009
    June 2009
    May 2009
    February 2009
    November 2008
    October 2008