How does unloading chunks work?

I have created a world and when I tp back to the default world and use unloadChunk it appears to be successfully done. But then if I run the command to unload chunks again, I can see that some of them are still loaded:


The number which are loaded gets lower over time and eventually goes to 0. Is this standard behaviour? Thanks.

Chunk unloading is a bit strange in minecraft. For a single chunk to unload. The connected chunks need to unloaded first.

Your code seems to be doing this, but 6, 0, 46 should be the one unloaded last. And then in theory, only that chunk would be fully unloaded. The fact more are unloaded i would put down to the connected chunks to them being unloaded too.

Sone code would be useful to know how your order works

1 Like

Ah interesting. Yea I was using the getLoadedChunks() method - and my code is here.

On the javadocs it says the order is undefined. Is it tricky to these chunks in the right order?

Based on your code it seems your unloading all the chunks if there are no players.

Im a bit confused on why this is needed as minecraft itself only loads chunks if a player is near it (the players render distance is whats used).

Is it a mod that is causing a issue where chunks arnt unloaded when a player leaves the world/server?

As for the order, yes its difficult as there could be … For lack of a better word … Patches of chunks (multiple sections of chunks).

So personally if your trying to unload all the chunks. I would do a while statement and just try to unload all the chunks. Only stop the while if jo chunks were unloaded in that pass or there are no chunks (as in check for both)

1 Like

I suppose I am in the chunks in this other world, and then when I tp back to the default world, they are still loaded for a bit? I don’t know really. For what it’s worth, that was on my own test server and the only other mod was worldedit lol.

What I find even more interesting is that this happens when I try to do the same thing on a server I know:

I tp back to the default world, and after a while of spamming the unload chunks command I am able to unload all of them. And then all of a sudden, without any player entering this other world, chunks suddenly appear to be loaded again. Guess that is some mod loading them back in? That’s like, really bad, right?

Anyway, just a curiosity. I will mark this as the solution for the original post because a while loop sounds like it will do the trick.

So there is a little time required for the server to work out that it doesnt need those chunks (ensuring entities are safely unloaded, no other players use that chunk, etc).

As for why its reloading the chunks. My guess is the world refreshed and loaded up the spawn chunks (spawn chunks dont like to be unloaded)

1 Like

Yea I think that’s right. Went out to like 5000 -5000 and the chunks that get loaded back in are spawn chunks (looked at Minecraft Coordinate Calculator lol)