java.lang.OutOfMemoryError Java heap space
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Java's java.lang.OutOfMemoryError: Java heap space
is a type of error that occurs when the Java Virtual Machine (JVM) cannot allocate an object because it has run out of memory and no more memory can be made available by the garbage collector. This is an indication that the heap space allocated for the running Java application is insufficient.
Understanding Java Heap Space
What is Heap Space?
Heap space in Java refers to a portion of memory allocated to Java applications during runtime. It is used for dynamic memory allocation, where new objects and class instances are created. Once objects are no longer referenced, they become eligible for garbage collection.
Causes of OutOfMemoryError
- Memory Leak: A memory leak occurs when objects are no longer in use but are not reclaimed by the garbage collector because they are still referenced. Over time, these leaking references consume the entire heap space.
- Improper Sizing: Configuring the JVM with insufficient heap size for the application's needs can result in an
OutOfMemoryError. - Excessive Object Creation: If the application logic creates a large number of objects in a very short period, it may overwhelm the available heap space before the garbage collector can reclaim memory.
- Large Data Structures: Handling large files, arrays, or collections that require more space than what is allocated can trigger this error.
- Third-Party Libraries: Poorly optimized third-party libraries or unanticipated behavior in external APIs can lead to excessive memory consumption.
Diagnosing the Error
Heap Dumps
A heap dump is a snapshot of all the objects in memory at a given time. It can be invaluable in diagnosing OutOfMemoryError
issues. After an error, a heap dump can be analyzed to find out which objects are consuming the most memory.
JVM Profiling Tools
Tools like VisualVM, JConsole, and Eclipse Memory Analyzer provide insights into memory allocation patterns, identifying bottlenecks or leaks.
Mitigating Strategies
Increase Heap Size
A straightforward response is to increase the heap space using JVM options:
-Xmsoption sets the initial heap size (e.g.,-Xms512mfor 512 MB).-Xmxoption sets the maximum heap size (e.g.,-Xmx1024mfor 1024 MB).
Optimize Code
- Review Data Structures: Use more memory-efficient data structures. For instance, prefer
ArrayListoverLinkedListwhen possible. - Use Efficient Iterators: Minimize object creation by reusing objects where possible instead of creating new ones.
- Defer Object Initialization: Delay the initialization of objects until they are absolutely necessary.
- Implement Caching Strategies: Make use of soft references and weak references to implement custom object caching mechanisms.
Garbage Collection Tuning
Improving the garbage collection process can help mitigate heap space issues. JVM provides various garbage collection algorithms and settings to fine-tune performance.
Key Points Summary
| Key Aspect | Description |
| Heap Space | Memory area for dynamic allocation of Java objects during runtime. |
| Common Causes | Memory leaks, improper sizing, excessive object creation, large data structures, third-party libraries |
| Diagnostic Tools | Heap dumps, VisualVM, JConsole, Eclipse Memory Analyzer |
| Mitigation Methods | Increase heap size, optimize code, garbage collection tuning |
Examples
Consider an application that processes large XML files. When these files are loaded into memory all at once, they can cause the JVM to run out of heap space:
Related reading
- Javascript algorithm to find elements in array that are not in another array
- Javascript Array.sort implementation?
- Javascript data structures library
- Javascript How to control flow with async recursive tree traversal?
- Javascript call() & apply() vs bind()?
- JavaScript How to download JS asynchronously?
- java.lang.OutOfMemoryError PermGen space in Maven build
- java.lang.OutOfMemoryError unable to create new native Thread

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.