
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the differences between StackOverflowError and OutOfMemoryError in Java?
A Java program may contain an interface, a variable, a method, a class, and an object. When we execute the program, the operating system allocates some memory to Java Virtual Machine. Then, JVM divides allocated memory into two parts, which are the Heap and the Stack.
The values of variables, methods, and classes are stored inside the Heap. And, the reference variables, method names, and classes are stored in the stack. When the Stack becomes full, the JVM throws a StackOverflowError, and when the heap becomes full, it throws an OutOfMemoryError.
StackOverflowError in Java
A stack is used for the execution of methods. It stores method parameters, local variables, and references to objects. When the Stack runs out of space, then a StackOverflowError occurs. It is a runtime error and hence, we can call it an exception.
When you run a Java program, a fixed stack size is allocated. During each method call, a call frame is created on the stack. Once the method execution completes, the call frame is removed from the stack.
If methods keep getting added to the call stack and no space is left for a new stack frame, then the program raises a StackOverflowError exception.
Example
Following Java program shows how StackOverflowError occurs:
public class Library { public static void addBooks(String studentName, int numBooks) { numBooks++; allocateBooks(studentName, numBooks); } public static void allocateBooks(String studentName, int numBooks) { try { numBooks++; int extraBooks = 3; int totalBooks = numBooks + extraBooks; // Printing total books System.out.println(studentName + " now has " + totalBooks + " books"); // This will cause StackOverflowError addBooks(studentName, totalBooks); } catch (StackOverflowError e) { e.printStackTrace(); } } public static void main(String[] args) { String studentName = "Ram"; int numBooks = 0; // infinite recursion addBooks(studentName, numBooks); } }
The above code will print the number of books, and when the stack becomes full, it will raise a StackOverflowError exception.
java.lang.StackOverflowError java.lang.StackOverflowError java.lang.StackOverflowError java.lang.StackOverflowError java.lang.StackOverflowError java.lang.StackOverflowError Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class java.lang.StackTraceElement$HashedModules Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "main"
The OutOfMemoryError
The objects we create in Java are stored in the heap memory. The "-Xmx" and "-Xms" attributes of the JVM determine its size. Unreferenced objects are removed by the Garbage Collector to free up space for new ones.
However, if too many referenced objects are created and memory is not being freed up, the heap will eventually run out of space, and then we encounter OutOfMemoryError.
Example
Let's see an example of OutOfMemoryError.
public class ErrorExample { public static void main(String[] args) { try { String stAray[] = new String[100 * 100 * 100000]; } catch(OutOfMemoryError exp) { exp.printStackTrace(); } } }
In the above code, the allocated size is greater than the heap size; therefore, we got the OutOfMemoryError as shown below:
javac ErrorExample.java && java -Xmx1024M -Xms256M ErrorExample java.lang.OutOfMemoryError: Java heap space at ErrorExample.main(ErrorExample.java:4)
StackOverflowError VS OutOfMemoryError
Now, let's discuss how StackOverflowError is different from OutOfMemoryError in Java:
StackOverflowError |
OutOfMemoryError |
---|---|
The StackOverflowError is related to the stack memory. |
The OutOfMemoryError is related to heap memory. |
It occurs due to the method call stack. |
It occurs due to object allocation. |
It cannot access private or default members from other packages. |
It cannot access private static members. |
Example: you can import classes like java.util.*, java.io.*, etc. |
Example: you can import static constants or methods like System.out, Math.PI, etc. |