
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What are the differences between StackOverflowError and OutOfMemoryError in Java?
Whenever we run a java program, the operating system allocates some memory to JVM. JVM divides this memory into two parts. One is Stack memory and another is Heap memory. A stack is used for the execution of methods and heap is used to store the objects. When the Stack becomes full, JVM throws java.lang.StackOverflowError and when the heap becomes full, JVM throws java.lang.OutOfMemoryError.
StackOverflowError
- A stack is used for the execution of methods. For every method call, one block is created in the stack memory
- The data related to the method like parameters, local variables or references to objects are stored in this block.
- When the method finishes its execution, this block is removed from the stack along with data stored in it.
- Whenever we call a method, it must finish its execution and leave the stack memory.
- If methods are staying in the stack then the stack will be full and JVM will throw java.lang.StackOverflowError.
OutOfMemoryError
- The objects we created in Java are stored in the heap memory. When the objects are no more required, they must be removed from the memory.
- The garbage collector removes the unwanted objects from the heap memory.
- If our objects have live references, the garbage collector doesn’t remove them. It removes only those objects which don’t have live references.
- Whenever we call a method, it must finish its execution and leave the stack memory.
- If there is no space left for new objects in the heap memory then JVM will throw java.lang.OutOfMemoryError.
- Related Articles
- What are the differences between C++ and Java?
- What are the differences between C and Java?
- What are the differences between Java classes and Java objects?
- What are the differences between recursion and iteration in Java?
- What are the differences between GridLayout and GridBagLayout in Java?
- What are the differences between JTextField and JTextArea in Java?
- What are the differences between JRadioButton and JCheckBox in Java?
- What are the differences between the TableCellRenderer and TableCellEditor in Java?
- What are the differences between ClassNotFoundException and NoClassDefFoundError in Java?\n
- What are the differences between length and length () in Java?\n
- What are the differences between JFrame and JDialog in Java?\n
- What are the differences between a class and an interface in Java?
- What are the differences between default constructor and parameterized constructor in Java?
- What are the differences between tight coupling and loose coupling in Java?
- What are the differences between compareTo() and compare() methods in Java?\n

Advertisements