Tuesday 17 November 2015

Have you faced OutOfMemoryError in Java? How did you solved that

OutOfMemoryError in Java is a subclass of java.lang.VirtualMachineError and JVM throws java.lang.OutOfMemoryError when it ran out of memory in heap.

OutOfMemoryError in Java can come any time in heap mostly while you try to create an object and there is not enough space in heap to allocate that object.

Two Types of OutOfMemoryError in Java:

1)
Java.lang.OutOfMemoryError: Java heap space : export JVM_ARGS="-Xms1024m -Xmx1024m"

2)
Java.lang.OutOfMemoryError: PermGen space : export JVM_ARGS="-XX:PermSize=64M -XX:MaxPermSize=256m"

Long Term Solution: Increasing the Start/Max Heap size or changing Garbage Collection options may not always be a long term solution for your Out Of Memory Error problem. Best approach is to understand the memory needs of your program and ensure it uses memory wisely and does not have leaks. You can use a Java memory profiler to determine what methods in your program are allocating large number of objects and then determine if there is a way to make sure they are no longer referenced, or to not allocate them in the first place.

No comments:

Post a Comment