The OutOfMemoryError (OOM) is thrown when the JVM cannot allocate an object because it has run out of heap memory and the garbage collector cannot free enough space. This is a serious error that usually indicates a memory leak or insufficient heap configuration.
# Increase heap size (temporary fix)
java -Xmx512m -Xms256m MyApplication
# For large applications
java -Xmx2g MyApplication
# Enable GC logging to diagnose
java -Xmx512m -verbose:gc -XX:+PrintGCDetails MyApplication
// ❌ Loading millions of records into memory
List<Record> allRecords = database.findAll(); // OOM for large tables!
allRecords.forEach(r -> process(r));
// ✅ Process in batches
int pageSize = 1000;
int page = 0;
List<Record> batch;
do {
batch = database.findAll(PageRequest.of(page++, pageSize));
batch.forEach(r -> process(r));
batch.clear(); // Help GC
} while (batch.size() == pageSize);
// ✅ Or use streaming (Spring Data)
database.streamAll().forEach(r -> process(r));
class Cache {
// ❌ Static map grows forever "” never cleared!
static Map<String, Object> cache = new HashMap<>();
static void add(String key, Object value) {
cache.put(key, value); // Memory leak!
}
}
// ✅ Use WeakHashMap "” entries removed when key is GC'd
static Map<String, Object> cache = new WeakHashMap<>();
// ✅ Or use a bounded cache with eviction
static Map<String, Object> cache = Collections.synchronizedMap(
new LinkedHashMap<>(100, 0.75f, true) {
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > 100; // Max 100 entries
}
}
);
// ✅ Or use Caffeine/Guava cache with TTL
Cache<String, Object> cache = Caffeine.newBuilder()
.maximumSize(1000)
.expireAfterWrite(10, TimeUnit.MINUTES)
.build();
// ❌ Reading entire 10GB file into memory
byte[] content = Files.readAllBytes(Paths.get("huge-file.csv")); // OOM!
// ✅ Stream lines one at a time
try (Stream<String> lines = Files.lines(Paths.get("huge-file.csv"))) {
lines.forEach(line -> processLine(line));
}
// ✅ Or use BufferedReader
try (BufferedReader reader = new BufferedReader(new FileReader("huge-file.csv"))) {
String line;
while ((line = reader.readLine()) != null) {
processLine(line);
}
}
"Java heap space" means the heap is full. "GC overhead limit exceeded" means GC is running constantly (>98% of time) but recovering less than 2% of heap "” effectively the heap is full but GC keeps trying.
Use JVM flags: -Xms for initial heap, -Xmx for maximum heap. Example: java -Xms256m -Xmx2g MyApp. For servers, set both to the same value to avoid heap resizing overhead.
Use profiling tools like VisualVM (free), JProfiler, or YourKit. Take heap dumps with jmap and analyze with Eclipse MAT. Look for objects that keep growing in count over time.
A memory leak in Java occurs when objects are no longer needed but are still referenced, preventing GC from collecting them. Common causes: static collections, unclosed streams, event listeners not removed, and ThreadLocal variables.
Metaspace stores class metadata. "OutOfMemoryError: Metaspace" occurs when too many classes are loaded (common with dynamic class generation or many classloaders). Increase with -XX:MaxMetaspaceSize=256m.
Explore 500+ free tutorials across 20+ languages and frameworks.