Minecraft Server Optimization Part 1: G1GC

Good Morning from my Robotics Lab! This is Shadow_8472, and today, I am working on my Minecraft server. Let’s get started!

A few months back, my sister put together a server idea she called Creepers ‘n’ Cream. It runs on Vanilla Minecraft, but has a few datapacks that offer us little gameplay changes, like mob and player heads, Elytra drop on a rematch with the dragon, and a tool for rotating some blocks in place. All I know is that she got the idea from Hermitcraft, a well known Minecraft server for YouTubers and streamers. We don’t run all of their packs.

I covered my process to get the server machine ready to host Creepers. it provided me with about six weeks of content, and in the end, I accepted MineOS as a much easier alternative to a practically custom-built Linux distro.

Once I got MineOS behaving with 16 GB of RAM open to it, I set it aside for a while. At one point, it had been running more or less stably for about two weeks without a reset when a couple players showed up unannounced. We had them contained in a house in adventure mode, so they were more or less contained. The server crashed unceremonially by a game tick (normally 1/20 of a second) lasting for a full minute. After a peek into the accounts’ history, it looked like they were hackers.

I don’t remember the second big crash all that well, but a couple weeks ago, it crashed the same way for a third time. I started doing some research, and found several places talking about garbage collection.

For the non-programmer, there’s a part of a program’s memory called the heap. If the heap ever overflows, the program crashes. As the program runs, objects are put into the heap when needed, and when they aren’t useful anymore, the program marks them as garbage. Every so often, a garbage collector comes by and tidies the heap up so it has more room for new objects. This takes time away from the program you want to be running. By default, Java collects all the garbage when the heap is full. Unfortunately for Minecraft, this causes a noticeable lag spike when garbage collection (GC) happens, and the bigger the heap, the longer it has to wait.

A number of posts I found in my research sung the praises of G1GC (Generation 1 Garbage Collection) for use with Minecraft. From what I gathered, G1GC focuses on making sure unused objects don’t clutter your heap for any longer than necessary. By having the collector come by frequently, it can collect in several short bursts too fast to notice at the trade off of taking a little longer overall.

Somewhere, I read a G1GC guide or two that suggested allocating all your RAM to Java when you start the program. Bad advice for default GC. The server was running a lot worse for a week before I reigned the minimum from 15 GB back to 4 GB.

Farther research into this theory led me to a programming Discord. Someone there informed me I almost certainly had a memory leak. On a trip to Third Workshop, I learned that even though two or more programs may be free from memory leaks their collaboration may introduce them.

I eventually switched over to basic G1GC. Where default GC would require daily restarts to remain in peak performance, G1GC kept the server stable for three days until planned maintenance took it offline.

In conclusion, I recommend all Minecraft server operators consider trying G1GC.

Final Question: Have you ever broke something by following poorly understood advice?

Leave a Reply