Fix “OutOfMemoryError: Java heap space” and “Could not reserve enough space” on a Minecraft server
Three different problems get called “out of memory”. If the server won't start and mentions the object heap, you asked Java for more RAM than the machine has. If it runs and then crashes with OutOfMemoryError, it needed more than you gave it, or something is leaking. If it just says “Killed”, the operating system ended it.
Which error do you have?
| Message | What it means |
|---|---|
| Could not reserve enough space for object heap | At startup: -Xmx is more than the memory available, or you run 32-bit Java. |
| java.lang.OutOfMemoryError: Java heap space | While running: the heap is full. The server needs more memory, less load, or has a leak. |
| GC overhead limit exceeded | Java spends nearly all its time clearing memory. It has the same causes as a full heap. |
| Killed, or exit code 137 | The operating system or the container ended the server because the whole machine ran out of memory. |
Could not reserve enough space for object heap
- Check the free memory. On Linux, run
free -h. On Windows, open Task Manager. - Lower -Xmx. Java uses more than the heap, so on a small machine keep -Xmx to about three quarters of the RAM at most. On a 4 GB server, 3G is a safe ceiling.
- Use 64-bit Java. The output of
java -versionshould say 64-Bit. A 32-bit Java cannot reserve a large heap.
java -Xms3G -Xmx3G -jar server.jar --nogui
Set -Xms to the same value as -Xmx, so the server reserves its memory once at the start.
java.lang.OutOfMemoryError: Java heap space
- Give the server more memory, if the machine has it. See how much RAM a Minecraft server needs for sizes by server type.
- Lower the load. Reduce
view-distanceandsimulation-distancein server.properties, for example to 8 and 6. - Look for a leak. Install the spark plugin or mod, let the server run until the memory is high, then run
/spark heapsummary. It lists what uses the most memory. Classes from one plugin at the top of the list point to the culprit. - Update or remove the culprit. Memory leaks are often fixed in a newer version of the plugin. Giving a leaking plugin more memory only delays the crash.
- Restart on a schedule. A daily restart hides a slow leak until you find it. It is a stopgap, not a fix.
“Killed” and exit code 137
This is not a Java error. On Linux, the out-of-memory killer ends the biggest process when the whole machine runs out of memory, and containers do the same at their memory limit. Confirm it in the kernel log:
sudo dmesg | grep -i "killed process"
To fix it, lower -Xmx so that Java plus everything else on the machine fits in the RAM, and stop other memory-hungry software, such as a database or a second game server. A small swap file works as a safety net, but never as a replacement for RAM.
Keep it from coming back
- Choose the heap size from your server type and player count, then watch real use with spark.
- Pre-generate the world, so chunk generation does not spike memory while players explore.
- Keep plugins and the server software up to date.
Host your Minecraft server in Singapore
Low ping for players in India, Malaysia and Indonesia, on fast AMD EPYC cores with NVMe and DDoS protection.
