Wednesday, May 9, 2018

How do you deal with OutOfMemoryError in java?

Problem statement: what is OutOfMemoryError and how to deal with OutOfMemmoryError?

OutOfMemoryError is thrown when there is insufficient space to allocate an object in java heap. In this case, garbage collector cannot make space available for new object and and thus it cause an error. It is said this is a serious problem and an application should not try to catch this error, rather size should be increased for JVM by using a flag named -Xmx (e.g. -Xmx1024M)

  • How to solve OutOfMemoryError in java?
          There are two types of OutOfMemoryError in java
  1. java.lang.OutOfMemoryError: Java heap space
  2. java.lang.OutOfMemoryError: PermGen space
  3. It can be solved by increasing the size of Java heap space and PermGen space using flag -Xmx and    -XX:MaxPermSize
  4. An important point to remember is that it doesn't depend on –Xmx value so no matter how big your total heap size you can run OutOfMemoryError in perm space. 
  5. The good thing is you can specify the size of permanent generation using JVM options "-XX: PermSize" and  "-XX: MaxPermSize" based on your project need.
  6. One small thing to remember is that "=" is used to separate parameter and value while specifying the size of perm space in the heap "=" is not required while setting maximum heap size in java it is required, as shown in below example.
export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m"

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...