In this post, I am going to explain the difference between StackOverFlowError and OutOfMemoryError. Let’s try to understand what is stack and Heap before dig further.
Stack and Heap memory
Stack is a special region in computer memory, that stores temporary variables like local variables, parameters, function calls etc., Stack works in last in first out (LIFO) order. Whenever a new local variable initialized inside a function, it is pushed onto the stack, when the function finished execution, all the local variables pushed by that function onto the stack are removed from the stack. Read this post for more details.
Heap is a portion of memory where dynamic allocation and de allocations happen. When the application request for a new object, JVM allocates the memory in Heap. Heap memory is slightly slower to read from and written to, because pointer access is needed to access heap memory. The JVM allocates Java heap memory from the OS and then manages the heap for the Java application. Refer this post for more details.
StackOverflowError
StackOverflowError is related to stack memory area and it is thrown when a stack overflow occurs because an application recurses too deeply.
Let’s try to produce StackOverflowError.
StackOverflowErrorDemo.java
public class StackOverflowErrorDemo {
static void fun() {
fun();
}
public static void main(String args[]) {
fun();
}
}
Compile and run above application.
$javac StackOverflowErrorDemo.java
$
$java StackOverflowErrorDemo
Exception in thread "main" java.lang.StackOverflowError
at StackOverflowErrorDemo.fun(StackOverflowErrorDemo.java:4)
at StackOverflowErrorDemo.fun(StackOverflowErrorDemo.java:4)
at StackOverflowErrorDemo.fun(StackOverflowErrorDemo.java:4)
at StackOverflowErrorDemo.fun(StackOverflowErrorDemo.java:4)
at StackOverflowErrorDemo.fun(StackOverflowErrorDemo.java:4)
at StackOverflowErrorDemo.fun(StackOverflowErrorDemo.java:4)
at StackOverflowErrorDemo.fun(StackOverflowErrorDemo.java:4)
As you see the class ‘StackOverflowErrorDemo’, ‘fun()’ method calls itself recursively infinite times, which leads to the stack gets exhausted and endup in StackOverflowError.
How to resolve StackOverflowError?
a. Check your application code for infinite recursions and resolve them.
b. If there is no infinite recursion, try increasing the stack size. Refer this post to increase the stack size.
OutOfMemoryError
OutOfMemoryError Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector.
OutOfMemoryError is related to Java Heapmemory. It typically occur, when the application request memory for a new object, but the heap is already full.
Let’s write a simple program to produce OutOfMemoryError.
OutOfMemoryErrorDemo.java
public class OutOfMemoryErrorDemo {
public static void main(String[] args) {
Integer obj[] = new Integer[Integer.MAX_VALUE];
for (int i = 0; i < Integer.MAX_VALUE; i++)
obj[i] = i;
System.out.println("I am done");
}
}
Compile and run the above program.
$javac OutOfMemoryErrorDemo.java
$
$java OutOfMemoryErrorDemo
Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
at OutOfMemoryErrorDemo.main(OutOfMemoryErrorDemo.java:4)
How to address the OutOfMemoryError?
a. First Profile your application and try to figure out, is your application referring any unused objects, or is there any memory leaks etc., You can refer this post to find the tools to analyze your java application.
b. Increase the heap size. Refer this post for more details.
References
https://docs.oracle.com/javase/7/docs/api/java/lang/StackOverflowError.html
https://docs.oracle.com/javase/7/docs/api/java/lang/OutOfMemoryError.html
You may like
Implement map using array as storage
How to resolve NoClassDefFoundError?
Get the length of longest row in a two dimensional jagged array in Java
No comments:
Post a Comment