Blog

Fewer crashes and less lag on every server

Free servers were being stopped mid-game, and a CPU setting made ticks stall. Here's what we changed on 29 November, and what changed after.

On 29 November I changed three settings on every server. In the three days before that, 108 free servers were stopped in the middle of a game because they ran out of memory. In the three days after, 3 were. And on a test server with the free plan's limits, the ticks you feel as lag went down to about a third.

None of this changes how the game plays. View distance, mob limits, redstone and farms all work exactly as they did. Here's what was wrong, what I changed, and how I measured it.

Free servers killed for running out of memory

Per day, on all three nodes

01020304050Change live21 Nov4722 Nov23 Nov24 Nov25 Nov26 Nov27 Nov28 Nov29 Nov130 Nov11 Dec
Hover or tap a day to see each node. The change went live at 08:40 UTC on 29 November and reached each server at its next start.
Show the numbers
Day (UTC)Helsinki node 1Helsinki node 2LondonTotal
21 Nov139527
22 Nov2218747
23 Nov751022
24 Nov87621
25 Nov47718
26 Nov83718
27 Nov1911434
28 Nov1842446
29 Nov81716
30 Nov1001
1 Dec1001

Why servers were being killed

Every server runs in a box with a memory limit: your plan, plus 1 GB we add on top for Java itself. On a free server that's 2 GB and 1 GB, so 3 GB in total. Java needs that extra room for things that aren't your world, like its own code, threads and network buffers. On real servers I measured between 380 and 810 MB of that.

The old start command let Java use 80% of the box for the world. On a free server that's 2.4 GB, which leaves only about 600 MB for everything else. Often that was fine. When it wasn't, the box went over 3 GB and Linux stopped the server on the spot. There was no save and no error in the console: the log just ended. Your players lost everything since the last autosave. One server's players told us their progress kept resetting every 5 to 10 minutes, and this was why.

Now Java gets exactly your plan for the world, 2 GB on the free plan, and the whole extra 1 GB is left for Java itself. If a server really needs more memory than its plan, it now runs out inside Java, and Java writes an error to the console. Before, it just disappeared.

I don't think this is a lucky week. There were about 13% fewer server starts in the three days after than in the three days before, but kills went from 108 to 3. Kills on bigger plans didn't move (37 before, 29 after), and it isn't zero for free servers either: three still ran out. I'll keep watching it.

Why a server pauses

A Minecraft server works in ticks: 20 a second, so each one has 50 ms. When a tick takes longer than that, the next one starts late, and players notice. Blocks pop back, mobs stutter, and you rubber-band a little.

Your plan also gives your server a share of CPU. On the free plan that's 1.25 cores. Linux keeps servers to their share in windows, and by default a window is 100 ms long. In each window, a free server may use 125 ms of CPU time, spread over all its threads.

That's the catch. A Minecraft server is never just one thread. The main thread runs the tick, and others make new chunks, save the world, send packets and clean up memory. When two or three of them are busy at once, the 125 ms is gone well before the window ends. Then every thread is paused until the next window starts, the main thread too. That pause can last 40 ms or more, in the middle of a tick.

With 10 ms windows, the server gets 12.5 ms of CPU every 10 ms. It's the same amount of CPU in total, but now the longest pause is a few milliseconds instead of most of a tick. Try it below.

Same CPU, different pauses

A tick that needs 20 ms of work, on a server limited to 1.25 cores

100 ms windows (before)This tick took 58 ms
10 ms windows (now)This tick took 31 ms
50 ms
The server may runPaused until the next windowThe tickThe tick, over 50 ms
A simplified model: the main thread and one more thread are busy the whole time. With 100 ms windows the tick takes anywhere from 20 to 58 ms, depending on when it starts. With 10 ms windows it takes 31 to 35 ms.

There's a small cost. Every tick now gets paused a little, often, instead of most ticks not at all and a few a lot. So the typical tick got slower: in the test below, the middle tick went from about 11 ms to 15 ms. That's still far below 50 ms, so you won't notice it. The slow ticks are the ones players feel.

How I measured it

I set up a test server with exactly the free plan's limits: 2 GB, 1.25 cores, the same box our servers run in, on Paper 26.3. Five players joined and then flew out in all directions at 8 blocks a second for two minutes. That makes the server create new land all the time, which is about the hardest a free server ever works. A small plugin wrote down how long each tick took.

Every run started from a fresh copy of the server. I ran the normal setup and each change three times in the same round, because the same setup can give very different results from one run to the next. This is a typical run of each. Drag the slider to choose what counts as slow.

Two minutes of ticks

Each bar is one tick of the test server, five players flying into new land

50 ms
Ticks slower than 50 msBefore: …Now: …
Hover over the bars to see a tick. The tallest bar in each pixel is drawn, so no spike is hidden.

Over six runs of each, the normal setup had 407 ticks over 50 ms per run on average. With 10 ms windows it was 129, a bit more than 3 times fewer. While the players were flying, one tick in a hundred took 82 ms or more before, and 64 ms or more now.

Two things didn't change. All runs kept 20 ticks a second on average, because a server catches up after a slow tick. That's why TPS alone doesn't show this kind of lag. The single slowest tick of a run didn't get better either: it was between 102 and 212 ms before and between 111 and 221 ms now.

Everything else I tried

Before settling on this I tried most of the things people usually suggest for a faster server. This chart has one dot per run. It also shows how much a result can change between runs: the normal setup itself ranges from 39% to 189% of its own average. So a change only clearly did something when its runs land below most of that range. Five of the six runs with 10 ms windows were better than the best run of the normal setup, and the sixth was better than all but two of its fifteen.

Everything I tried

Slow ticks in each run, compared with the normal setup in the same round (100%)

0%50%100%150%200%Leaf instead of PaperCPU windows of 10 msCPU windows of 5 msCPU windows of 20 msCompact object headersNo compression to the proxyThe normal setupThe old memory settingTuned garbage collector flagsCPU burstlz4 world filesFewer slow ticksMore slow ticks
One runThe middle runWhat we use now
Each round of runs had its own normal setup, and each dot is compared with that round's average. Hover or tap a dot for its run.
Show the numbers
SettingSlow ticks per runMiddle run
Leaf instead of Paper657, 123, 15424%
CPU windows of 10 ms147, 97, 128, 119, 190, 9230%
CPU windows of 5 ms120, 116, 15044%
CPU windows of 20 ms161, 139, 22960%
Compact object headers139, 318, 73067%
No compression to the proxy220, 339, 78486%
The normal setup210, 733, 486, 316, 711, 152, 670, 612, 658, 140, 159, 512, 211, 512, 540102%
The old memory setting795, 211, 499105%
Tuned garbage collector flags138, 818, 546115%
CPU burst517, 783, 191132%
lz4 world files648, 719, 798183%
  • Garbage collector flags, including the well-known Aikar flags that a lot of hosts advertise, made no difference next to Java's own defaults. ZGC and Shenandoah used about 2.7 GB at their peak in a 3 GB box, which is far too close to the limit.
  • Windows of 20 ms and 5 ms both helped, but 10 ms helped the most.
  • CPU burst lets a server save up CPU while it's quiet and spend it later. It didn't help here, because a busy server never has anything saved up. We still leave it on.
  • Leaf did well in two of its three runs. It's already on the create page if you want to try it, but it changes some vanilla behaviour to be faster, so check that it suits your server first.
  • lz4 world files and compact object headers made no clear difference.

The third change

Your server used to compress everything it sent to our proxy. The proxy then unpacked it and compressed it again for each player, so all of it was compressed twice. Now servers send it to the proxy uncompressed. That saves about 1% of a busy server's CPU, plus the proxy's work of unpacking it. It won't change lag, and you can see in the chart that it didn't. Servers in London keep compressing, because they're hosted on a home internet connection, and players who join through Helsinki reach them over it.

What you need to do

Nothing. Every change applies the next time your server starts, so if you've restarted it since 29 November, you already have them.

If your server still crashes, the console's "Why did my server stop?" card will try to tell you why. If it still lags, tell me on Discord: what kind of server it is, and when it happens.

Make your server

It takes a few minutes and costs nothing.