Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between StackOverflowError and OutOfMemoryError

Tags:

java

What's the difference between StackOverflowError and OutOfMemoryError and how to avoid them in application?

like image 599
Ifozest Avatar asked Jul 11 '12 15:07

Ifozest


People also ask

What does StackOverflowError mean?

A stack overflow is a type of buffer overflow error that occurs when a computer program tries to use more memory space in the call stack than has been allocated to that stack.

What is StackOverflowError in Java?

StackOverflowError is a runtime error which points to serious problems that cannot be caught by an application. The java. lang. StackOverflowError indicates that the application stack is exhausted and is usually caused by deep or infinite recursion.

What is OutOfMemoryError?

OutOfMemoryError is a runtime error in Java which occurs when the Java Virtual Machine (JVM) is unable to allocate an object due to insufficient space in the Java heap. The Java Garbage Collector (GC) cannot free up the space required for a new object, which causes a java. lang.


2 Answers

Short answer:

  • OutOfMemoryError is related to Heap.
  • StackOverflowError is related to stack

Long answer:

When you start JVM you define how much RAM it can use for processing. JVM divides this into certain memory locations for its processing purpose, two of those are Stack & Heap

If you have large objects (or) referenced objects in memory, then you will see OutofMemoryError. If you have strong references to objects, then GC can't clean the memory space allocated for that object. When JVM tries to allocate memory for new object and not enough space available it throws OutofMemoryError because it can't allocate the required amount of memory.

How to avoid: Make sure un-necessary objects are available for GC

All your local variables and methods calls related data will be on the stack. For every method call, one stack frame will be created and local as well as method call related data will be placed inside the stack frame. Once method execution is completed, the stack frame will be removed. ONE WAY to reproduce this is, have an infinite loop for method call, you will see stackoverflow error, because stack frame will be populated with method data for every call but it won't be freed (removed).

How to avoid: Make sure method calls are ending (not in an infinite loop)

like image 127
kosa Avatar answered Sep 20 '22 07:09

kosa


Imagine you have a function like the following

public void f(int x) {     return f(x + 1); } 

When you'll call it the call will call f again and again and again. At each call a bit of information is stored on the stack. Since the stack is limited in size you will get a StackOverflowError.

Now imagine the following code:

for (int i = 1; i > 0; i++)     vector.add(new BigObject()); 

where BigObject is a normal Java object. As you see, the loop never terminates. Each allocation is done on the heap thus it will be filled with BigObjects and you will get an OutOfMemoryError.

To recap:

  • OutOfMemoryError is thrown when you are creating objects
  • StackOverflowError is thrown when you are calling functions
like image 29
Mihai Maruseac Avatar answered Sep 20 '22 07:09

Mihai Maruseac