Tuesday, October 25, 2016

java.lang.OutOfMemoryError: Java heap space

Out Of Memory issue occurred mostly at 2 places.
1)    The java.lang.OutOfMemoryError: Java heap space
2)    The java.lang.OutOfMemoryError: PermGen space

Before discussing the OutOfMemory, would like to give brief detail about heap and non-heap spaces.

Heap
The JVM has a heap that is the runtime data area from which memory for all class instances and arrays are allocated. It is created at the JVM start-up.

If you are familiar with different generations on the heap.
You may aware about new, old and permanent generation of heap space.

Non-Heap
The JVM has memory other than the heap, referred to as non-heap memory. It is created at the JVM startup and stores per-class structures such as runtime constant pool, field and method data, and the code for methods and constructors, as well as interned Strings.

2nd issue or PermGen Space related is coming when Non-Heap Memory is full.

1st Reason is:-
Permanent generation(PermGen) of the heap is used to store String pool and various Metadata required by JVM related to Class, method and other java primitives.

If Project is big, you can easily run out of memory if you have too many unnecessary classes or a huge number of Strings in your project and also not managing object creation.
To resolve this kind of issue you can increase the size of permGen by
export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m

2nd Reason is:-
Memory leak through classLoader.
Most likely occurred by webserver or application server.

Sharing some points with you.

1)Check the Jconsole(for running jconsole, just open cmd and type jconsole&)


In highlighted area you can see the heap and non-heap memory is too high and can cause OutOfMemory.

As we know GC is being called and it will release the space in heap, you can see the figure attached below.
Here I came to know that, server is loading same class again and again on each job run.
So for solving this ,I restrict the class to load again and removed unnecessary String literal also.


So solution can be used:

1) Remove any class which load every time on each call,for an example loading JWSDynamicClientFactory for each soapcall.
2)Remove any unused or unnecessary String creation.
3) Check File I/O operation id happening properly or not


Any suggestion and Questions is most welcome.

Thanks,
Vikas




No comments:

Post a Comment