twitter
    !! Tell us what you want to Learn / Know !!

Out Of Memory Issues

"One day Every java Programmer will face the Out Of Memory Exception". I just want to make clear that there are many reasons that cause the Out Of Memory Exception. Below are some common OOM issues.

Heap:
      In the heap we get an OutOfMemoryError, if the garbage collector cannot reclaim enough memory for a new object. In such situation the Sun HotSpot JVM shows this error message:

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

An alternative for this is

Exception in thread "main": java.lang.OutOfMemoryError: Requested array size exceeds VM limit

If the application tries to create an array on the heap that is bigger than the total heap size.

Solution:
        First thing that needs to be checked is the gc logs. Need to check whether the garbage collection is happening properly. If the heap keeps gradually increasing even after full gc, tune the gc algorithms and check if the behavior is the same. Use memory leak detector tools for both sun jdk and JRockit to check which instances from the application are not getting destroyed.
If this is not the case and the application genuinely needs more memory, increase the heap size by using the parameters:

Example: -Xms2048m -Xmx2048m

PermGen:
         If there is not enough memory in the method area for creating a new class, the Sun HotSpot implementation gets an error in the permanent generation:

Exception in thread "main": java.lang.OutOfMemoryError: PermGen space

Solution:
        Increase the max permgen space -XX:MaxPermSize=256m
There can be a leak in the permgen objects. If tuning parameters do not resolve the issue, we need to use the memory leak detector tools and find out which instances in the permgen space are not getting cleared.

Threads:
        OutOfMemory errors in thread exclusive memory areas occur less frequently and are identified by the following error messages in the Sun HotSpot JVM:

Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread

Exception in thread "main": java.lang.OutOfMemoryError: <reason> <stacktrace> (Native method)

The first error is thrown if there are too many threads in the JVM and there is not enough memory left to create a new thread. I’ve seen this because the memory limits of a process have been reached (especially in 32bit operating systems, e.g. on Windows 32bit it is 2GB) or the maximum number of file handles for the user that executes the java process has been reached. The second error message indicates that a memory allocation error on a native stack (JNI method call) has occurred.

Solution:
1)Set kernel parameter maxdsiz to a higher value
2) Reduce the current heap size.
3) Check the kernel values:  ulimit -a
4) If the NPROC soft limit is lower than the hard limit, increase it as needed: ulimit -u <new value>.
Check the Operating System documentation to make changes permanent at the OS configuration files.
5) Need to reduce the JVM stack size and the OS stack size both. Set the -xss in java options and set ulimit -s on the OS level.

Stack Over Flow:
         It is also interesting that a memory allocation error on the JVM stack (too many frames on the stack) does not throw a Java OutOfMemory error but as the JVM specification mandates: java.lang.StackOverflowError.

Solution:
        Stack over flow error is usually generated due to a recursive call made by the application (infinite recursion), or it’s because of an attempt to allocate more memory on the stack than will fit. This is usually the result of creating local array variables that are far too large for the current stack.
For the first possibility, we need to check the application code as to where is the recursive call being made.
For the second possibility, we can increase the JVM stack size by the parameter : Example: -Xss512K


Swap Space:
         Exception in thread "main": java.lang.OutOfMemoryError: request <size> bytes for <reason>. Out of swap space?

This error is thrown if there is not enough memory left on the operating system level – which is normally true if other processes are using all of the available memory or the swap space is configured too small.
This indicates Java has failed to acquire more memory from the operating system.

Possible causes:
- not enough swap space left, or
- kernel parameter MAXDSIZ is very small.

Solution:
If you see this symptom, consider increasing the available swap space by allocating more of your disk for virtual memory and/or by limiting the number of applications you run simultaneously. You can also decrease your usage of memory by reducing the value of the -Xmx flag, which limits the size of the Java object heap.
This issue is being tracked in bug 4697804.

References: 
Oracle
Code Centric
weblogic wonders


2 comments:

deepak said...

good one mister refreshed my experiences.thanks

Surendra said...

Hi deepak thanks for your feedback.. You r always welcome...

Post a Comment