Search code examples
javamemorydeploymentserverout-of-memory

Is memory out of bound exception or error?


Is memory out of bound exception or error? We usually get this during project deployment on the server. It might be a basic question. I googled it but I could not find relevant answer so posting here.

Error I got:

Invocation of init method failed; nested exception is java.lang.OutOfMemoryError: allocLargeObjectOrArray - Object size: 8216, Num elements: 2049

And how can we handle this?


Solution

  • java.lang.OutOfMemoryError extends java.lang.Error and not java.lang.Exception

    catching Exception you will miss it

    try{
    ....
    }catch(Exception ex){
     //will not catch OutOfMemoryError, since it does not extend Exception
    }
    

    catching Throwable, you will hit'em both..

    try{
    ....
    }catch(Throwable ex){
     //will catch both Exception and OutOfMemoryError, they both extend this
    }
    

    Whether it is good or not to catch Throwable is another question see this Is it a bad practice to catch Throwable? (Thanks to @Dawnkeeper for link)