Minecraft server memory, garbage collection and JVM flags
How Java memory works on a Minecraft server, why it crashes with OutOfMemoryError, how garbage collection causes lag spikes, and what Aikar's flags do.
Updated · 3 min read
A Minecraft server runs on Java, which manages memory in a "heap" and periodically cleans it up with a garbage collector (GC). Running out of heap causes crashes, and long GC pauses cause lag spikes. Getting memory right is mostly about giving the server enough, not too much, and fixing whatever is filling it.
How memory is set on Schokr
Each server gets a memory amount from your team's pool (6 GB total on the free package). The template's startup command uses that amount to set Java's maximum heap. You can see the full startup command and its variables in your server's Startup settings.
Not all of a server's memory is heap. Java also needs memory for its own code, threads and native buffers, so the heap is kept a bit below the server's total memory. This is normal, and it is why the server should not be configured to use 100% of its memory as heap.
Two kinds of "out of memory"
1. Java heap full
java.lang.OutOfMemoryError: Java heap space
The heap filled up. The server usually crashes or becomes unresponsive. Causes include too many loaded chunks, a memory-hungry plugin or mod, a memory leak, or simply not enough memory for the modpack.
2. Container limit reached
The server stops suddenly with no Java error, often while memory usage in the console was near its maximum. The whole process exceeded the server's memory limit and was stopped. This usually means heap plus Java overhead was more than the server had.
Fixes
- Give the server more memory from your team's pool, or take some from another server. Store upgrades add 1 GB for 450 credits.
- Lower
view-distanceandsimulation-distanceso fewer chunks stay loaded. - Remove or update the plugin or mod that uses the most memory.
/spark heapsummaryshows what is filling the heap. - Restart regularly if a plugin leaks memory, and report the leak to its author.
Garbage collection and lag spikes
Java frees memory in GC cycles. With a good collector these are short, but if the heap is nearly full all the time, the GC runs constantly and can pause the server for hundreds of milliseconds. You see this as regular freezes, Can't keep up! messages and saw-tooth memory graphs that never drop far.
Check with:
/spark gc
/spark health
Many frequent collections or long pause times point to a heap that is too small for the load.
What about too much memory?
More memory is not always faster. A huge heap with very little garbage can lead to longer, rarer pauses, and memory you give one server is memory your other servers in the team cannot use. Give the server what it needs plus some headroom, and spend the rest elsewhere.
Aikar's flags
"Aikar's flags" are a well-known set of JVM options for Minecraft servers that tune the G1 garbage collector for short, predictable pauses. They set things like the target pause time and how the heap is divided, and they are recommended in Paper's documentation. Modern Java versions already use G1 by default, so the biggest gains come from having the right memory amount; the flags fine-tune GC behaviour on top of that.
Whether you can add custom JVM flags depends on what your template's Startup settings let you edit. If you cannot change the startup command yourself and think you need custom flags, ask in the Schokr Discord. If you can, copy the current flags from Paper's documentation rather than an old forum post, and keep the memory values in line with your server's allocation.