mojira.dev
MC-245406

Memory leak leading to crashes when loading pre-1.18 chunks

For chunks generated in Minecraft 1.17.1 (and presumably older versions) and subsequently loaded using Minecraft 1.18.1, memory usage rapidly increases until the server crashes. This only happens the first time these chunks are loaded.

This is reproducible in vanilla with no mods by loading a pre-1.18 world with a large amount of generated chunks, and then causing a large amount of these old chunks to load. Although this is reproducible and has been tested to happen in a vanilla environment, a "world pregenerator" type mod which loads lots of chunks quickly is the quickest way to reproduce this issue.

This was reported to Paper as 7094, and I have debugged the source to be ChunkAccesses holding onto NoiseChunks, which hold a Blender, which references a WorldGenRegion, which of course holds a list of ChunkAccess.

The following diff solved the issue:

diff --git a/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java b/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
index 96cb3e8cad9e7a5edd2a448ea88f2447104fbb5a..86cbb683f3e7292d29fc6bea1fb9ffaa018b3254 100644
--- a/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
+++ b/src/main/java/net/minecraft/world/level/chunk/ChunkAccess.java
@@ -410,11 +410,7 @@ public abstract class ChunkAccess implements BlockGetter, BiomeManager.NoiseBiom
     }
 
     public NoiseChunk getOrCreateNoiseChunk(NoiseSampler noiseColumnSampler, Supplier<NoiseChunk.NoiseFiller> columnSampler, NoiseGeneratorSettings chunkGeneratorSettings, Aquifer.FluidPicker fluidLevelSampler, Blender blender) {
-        if (this.noiseChunk == null) {
-            this.noiseChunk = NoiseChunk.forChunk(this, noiseColumnSampler, columnSampler, chunkGeneratorSettings, fluidLevelSampler, blender);
-        }
-
-        return this.noiseChunk;
+        return NoiseChunk.forChunk(this, noiseColumnSampler, columnSampler, chunkGeneratorSettings, fluidLevelSampler, blender); // Paper - create a new one each time to avoid leaking
     }

However I don't know enough about how and when the NoiseChunk is used to know if this is a correct solution, and as noted in this PR, it's probably best to clear the reference once the NoiseChunk is "done" being used rather than creating a new one each time getOrCreate is called, however I am not sure where/when the NoiseChunk is "no longer needed".

Comments 4

Does MC-243471 describe your issue?

I don't think MC-243471 is the same issue. That report details memory usage exceeding the allocated amount, which doesn't happen in the case of this issue, where the heap fills up to the allocated size, and then the server crashes. This issue also does not entail a system wide freeze like the other issue does, and is happening for the vanilla overworld dimension, contrary to the comment from yitzy299 on the other issue.

Two questions:

Do you have a world you can attach?

Does this also happen in a Windows environment?

I've generated and attached a world as requested: https://www.mediafire.com/file/l1jvhcj9w2g92o2/world.zip/file (uploaded to fileshare site as it is 100mb), but it should be noted that there is nothing special whatsoever about this world - it is a standard 1.17.1 world generated from a random seed. The issue is reproducible using any 1.17.1 (and possibly prior) world, using my specific world is not needed.

 

As far as whether this happens in a Windows environment, I haven't tested it myself as I don't normally use Windows, but I don't think any part of this issue is platform-specific, I would expect the same results across macOS, Linux, BSD, Windows, etc.

jmp

Panda4994

Plausible

Very Important

Crash

crash, memory-leak

1.18.1

22w03a

Retrieved