OutOfMemoryError
Java Programming Language
Severity: CriticalWhat Does This Error Mean?
An OutOfMemoryError means the Java Virtual Machine (JVM) ran out of memory and could not allocate space for a new object. The most common form is 'Java heap space' — the area where Java stores objects is full. This can happen because your program uses too much data, has a memory leak, or the JVM was not given enough memory to start with.
Affected Models
- Java 8
- Java 11
- Java 17
- Java 21
- All Java versions
Common Causes
- A memory leak — objects are created and added to a collection but never removed, growing forever
- Loading a very large file or dataset entirely into memory at once instead of processing it in chunks
- The JVM heap size is too small for the workload — the default may not be enough for your application
- Generating too many objects too quickly, overwhelming the garbage collector
- A 'PermGen space' or 'Metaspace' error — too many classes loaded, often in application servers with hot-reload
How to Fix It
-
Read the full error message — it specifies which memory area ran out. 'Java heap space' means object memory. 'Metaspace' means class metadata. Each has a different fix.
The most common is 'Java heap space'. This guide focuses on that case.
-
Increase the JVM heap size as a first step. Start Java with -Xmx to set the maximum heap: for example, -Xmx512m for 512 MB or -Xmx2g for 2 GB.
In application servers like Tomcat, set this in the startup script. In Maven: set MAVEN_OPTS=-Xmx1g. This buys time while you find the root cause.
-
Look for memory leaks — places where objects are added to a list, map, or cache but never removed. Static collections are a common culprit: they live for the lifetime of the application.
A sign of a leak is that memory usage keeps climbing over time and never drops back down even after the work is done.
-
Avoid loading large files or datasets entirely into memory. Use streaming APIs to process data in chunks — read a line, process it, move on to the next without keeping all lines in memory.
For large files: use BufferedReader line by line instead of Files.readAllLines(). For databases: use pagination instead of loading all rows at once.
-
Use a memory profiler to take a heap dump and see what objects are consuming memory. Generate a heap dump with -XX:+HeapDumpOnOutOfMemoryError and analyze it with Eclipse MAT or VisualVM.
The profiler will show you which object types are taking the most space and where they were created — this points directly to the leak.
When to Call a Professional
OutOfMemoryError is serious but fixable. First try increasing JVM memory. If the error keeps coming back, you likely have a memory leak that needs profiling. Tools like VisualVM, JProfiler, or Eclipse MAT can help you find what is holding memory.
Frequently Asked Questions
What is the difference between heap space and stack space in Java?
Heap space is where Java stores all objects — things you create with the 'new' keyword. Stack space is where Java stores method calls and local variables. OutOfMemoryError: Java heap space means too many objects. StackOverflowError means too many nested method calls.
Why does Java not free up memory automatically when it runs low?
Java does have automatic garbage collection — it reclaims memory from objects that are no longer referenced. But if your code keeps references to objects (even accidentally, through a list), the garbage collector cannot free them. Garbage collection only frees objects that nothing points to anymore.
What does 'GC overhead limit exceeded' mean?
This is a specific type of OutOfMemoryError that happens when the garbage collector is working almost constantly but cannot free enough memory to make progress. Java triggers this when more than 98% of CPU time is spent on garbage collection. It means your program is creating objects so fast that GC cannot keep up — usually a sign of a memory leak.