This discussion is archived
9 Replies Latest reply: Nov 27, 2012 5:53 PM by jschellSomeoneStoleMyAlias RSS

Catch an OutOfMemoryError Exception

975293 Newbie
Currently Being Moderated
Hy,

I created an infinite loop creating objects inside this loop in a thread to raise on porpuse an OutOfMemoryError and I am catching this exception but I have been reading that we should not catch this exception because it would be useless try to do something because there wouldnt be memory available, right? but when this exception is raised and it goes to the "catch section", I have checked doing "Runtime.getRuntime().freeMemory()" in this section that free memory is available again so:

1. I suppose the GC removes objects when OutOfMemoryError is raised liberating memory so : why not to catch this OutOfMemoryError exception if the GC will remove objects liberating memory?

Thanks
  • 1. Re: Catch an OutOfMemoryError Exception
    gimbal2 Guru
    Currently Being Moderated
    The JVM will try to do a full garbage collection run to free up memory.The exception is only thrown when the JVM cannot free enough memory.

    Now as a result of the exception being raised and execution being interrupted, objects created locally will go out of scope and be illegible for garbage collection. That may free up the memory you're seeing. But the JVM will NOT arbitrarily throw away objects, only the ones that are not referenced anywhere.
  • 2. Re: Catch an OutOfMemoryError Exception
    975293 Newbie
    Currently Being Moderated
    Ok,

    I have a servlet and has this structure:

    protected void doPost(HttpServletRequest req, HttpServletResponse resp){

    try{     

    *//some code, for example: creation of many objects that will raise OutOfMemoryError*




    }catch (OutOfMemoryError e)
    *//When gets here, there can not be references to any of the objects created inside the "try" block so the GC will always remove the objects liberating //memory, right?*

    }
    }
  • 3. Re: Catch an OutOfMemoryError Exception
    Kayaman Guru
    Currently Being Moderated
    972290 wrote:
    //When gets here, there can not be references to any of the objects created inside the "try" block so the GC will always remove the objects liberating //memory, right?
    Basically yes. But catching an OutOfMemoryError is a design flaw in most cases and is in no way a reliable way to handle things.
  • 4. Re: Catch an OutOfMemoryError Exception
    baftos Expert
    Currently Being Moderated
    private BigObject bo1;
    
     protected void doPost(HttpServletRequest req, HttpServletResponse resp){
     
         try{      
           
            this.bo1 = new BigObject();
            BigObject bo2 = new BigObject(); // This will generate OOME
     
     
        }catch (OutOfMemoryError e)
           // The object referenced by bo2 has been garbage collected, the object referenced by bo1 not at all
        }
     }
  • 5. Re: Catch an OutOfMemoryError Exception
    976211 Newbie
    Currently Being Moderated
    Yes it is possible to catch OutOfMemoryError but it will not prevent your program from stopping.
    Or you can simply force your JVM to garbage collect the objects by JVM but this doesn't make sure that the objects will be garbage collected.

    The possible answer to this can be "Try to simplify your code and minimize the number of objects".
  • 6. Re: Catch an OutOfMemoryError Exception
    gimbal2 Guru
    Currently Being Moderated
    973208 wrote:
    Yes it is possible to catch OutOfMemoryError but it will not prevent your program from stopping.
    Or you can simply force your JVM to garbage collect the objects by JVM but this doesn't make sure that the objects will be garbage collected.
    And you can't force anything either, you can only "hint". As I already stated, forced garbage collection does not prevent out of memory errors. Freeing up references to objects that use lots of memory does.
    The possible answer to this can be "Try to simplify your code and minimize the number of objects".
    Both of which do not really help. Its about a large amount of memory being used, not about complexity of code or number of created objects. Thousands of small object still don't count up to a high memory usage.
  • 7. Re: Catch an OutOfMemoryError Exception
    975293 Newbie
    Currently Being Moderated
    Hy, just one more question. Its said that the objects created by a thread are stored in the heap memory, and this heap memory is shared between Threads but what kind of objects are stored in the heap memory, the ones which are created inside a method or objects that are class properties or both? In the next code I put two examples:

    private BigObject bo1 = new BigObject(); *=> will store in heap memory?*

    protected void doPost(HttpServletRequest req, HttpServletResponse resp){

    try{      

    BigObject bo2 = new BigObject(); *=> will store in heap memory?*


    }catch (OutOfMemoryError e)
    // The object referenced by bo2 has been garbage collected, the object referenced by bo1 not at all
    }
    }



    Thanks
  • 8. Re: Catch an OutOfMemoryError Exception
    Kayaman Guru
    Currently Being Moderated
    972290 wrote:
    what kind of objects are stored in the heap memory
    All objects are stored on the heap.
  • 9. Re: Catch an OutOfMemoryError Exception
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    972290 wrote:
    Hy,

    I created an infinite loop creating objects inside this loop in a thread to raise on porpuse an OutOfMemoryError and I am catching this exception but I have been reading that we should not catch this exception because it would be useless try to do something because there wouldnt be memory available, right? but when this exception is raised and it goes to the "catch section", I have checked doing "Runtime.getRuntime().freeMemory()" in this section that free memory is available again so:

    1. I suppose the GC removes objects when OutOfMemoryError is raised liberating memory so : why not to catch this OutOfMemoryError exception if the GC will remove objects liberating memory?
    The real problem is not as you stated. When you consider a single thread then it would be possible for the VM to unroll the stack and thus there would be more memory available.

    However all VMs (excluding perhaps some limited real time ones) do not have one thread.
    And often the real cause of a OOME is not something that can be collected by the VM.

    So you have the possibility that the exception might occur somewhere besides where you might 'expect' it and/or no way to fix it.

    That said you can catch it and you can try to do something, like log the error. It might fail but if it succeeds then you will at least know why the application exited. If it fails to log then you are no worse off than if you didn't try to catch it in the first place.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points