How to Fix java.lang.OutOfMemoryError in Minecraft
Complete guide to fixing java.lang.OutOfMemoryError in Minecraft clients and servers. Covers heap tuning, memory leaks, GC flags, and mod memory optimization for Java Edition 1.21+.
How to Fix java.lang.OutOfMemoryError in Minecraft
The Error
java.lang.OutOfMemoryError: Java heap space
at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:...)
...
The server has run out of memory. Consider increasing the maximum heap size.
This error occurs when the Java Virtual Machine exhausts all allocated heap memory and cannot create new objects. It can crash both Minecraft clients and dedicated servers. The result is an immediate shutdown or a frozen state where garbage collection runs continuously but frees nothing.
Quick Fix
Open your launch script or launcher JVM arguments and increase the -Xmx value. For a client, 4 GB is a reasonable starting point. For a server with plugins, 6-10 GB is typical. Always set -Xms equal to -Xmx to prevent heap resizing overhead.
java -Xms6G -Xmx6G -jar server.jar nogui
Step-by-Step Diagnosis
- Check current allocation. Look at your start script or launcher profile for the
-Xmxflag. If it is below 2 GB for a client or below 4 GB for a server, increase it first. - Monitor real-time usage. Press F3 in the client to see memory usage in the debug screen. On a server, install Spark and run
/spark healthto see heap utilization. - Profile with a heap dump. Add
-XX:+HeapDumpOnOutOfMemoryErrorto your JVM flags. When the OOM occurs, the JVM writes a.hproffile. Open it in Eclipse MAT or VisualVM to find which objects consume the most memory. - Check loaded chunks. Excessive view distance or force-loaded chunks inflate memory quickly. Reduce
view-distanceto 8 andsimulation-distanceto 6 inserver.properties. - Audit plugins and mods. Some plugins cache data per player without cleanup. Remove plugins one at a time and monitor heap usage over 30 minutes to identify the offender.
- Verify Java version. Java 21 has improved garbage collection compared to Java 17. Use
java -versionto confirm. Minecraft 1.21+ requires Java 21.
Common Causes
- Insufficient heap allocation. The most common cause. Default launcher settings often use only 2 GB, which is not enough for modded clients or busy servers.
- Memory leaks in plugins. Plugins that store references to Player objects after disconnection prevent garbage collection. Leak-prone plugins include those with per-player caches, scoreboards, or custom inventories.
- Too many loaded chunks. Each chunk uses 10-20 KB of heap. A view distance of 16 with 40 players can load over 50,000 chunks, consuming several gigabytes of RAM.
- Large modpacks. Modpacks with 200+ mods require 8-12 GB of heap. Texture-heavy mods and mods that register thousands of custom blocks are the biggest consumers.
- World size. Extremely large worlds with many explored chunks load more data from disk. Pre-trimming old chunks with MCA Selector can reduce memory pressure.
Version-Specific Notes
Minecraft 1.21+ requires Java 21. The G1 garbage collector is the default and recommended choice. ZGC is available as an alternative for servers with 16 GB+ heap, using -XX:+UseZGC. Avoid CMS, which was removed in Java 14.
Paper 1.21+ includes async chunk loading that reduces peak memory usage compared to Spigot. If you are running Spigot and hitting OOM errors, switching to Paper is one of the most effective fixes.
Recommended JVM Flags
java -Xms8G -Xmx8G -XX:+UseG1GC -XX:+ParallelRefProcEnabled \
-XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions \
-XX:+DisableExplicitGC -XX:+AlwaysPreTouch \
-XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 \
-XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 \
-jar server.jar nogui
FAQ
Can I allocate too much RAM to Minecraft?
Yes. Over-allocating RAM (for example, 32 GB for a small server) causes long GC pauses because the garbage collector has more memory to scan. Allocate only what your server actually needs, then add a 20% buffer.
Does this error corrupt my world?
An OOM crash during chunk saving can cause chunk corruption. Always keep backups. If your world fails to load after an OOM crash, restore from the most recent backup.
Is this different from a stack overflow error?
Yes. OutOfMemoryError means heap space is exhausted. StackOverflowError means a method recursed too deeply. They have different causes and fixes.
Should I use -XX:+UseZGC instead of G1GC?
ZGC is designed for ultra-low pause times on large heaps (16 GB+). For most Minecraft servers, G1GC with Aikar's flags performs better. ZGC can increase overall CPU usage in exchange for shorter pauses.
Need a server that handles all this? Astroworld Hosting, NVMe SSDs, Pterodactyl panel, DDoS protection on every plan.