Jump to content

[1.8][SOLVED] How to remove chunks from ChunkProvider cache?


Recommended Posts

I have a (server-side) mod which is accessing a large number of chunks when it activates.  Once accessed, these chunks do not need to remain in the cache as they are not likely to be accessed again for a long time.


The problem is that the Java heap memory runs out.  This is because the IChunkProvider.getLoadedChunkCount() shows me that I have many thousands of chunks still in the cache, exhausting the available memory.


My question is, how can I kick these out of the cache?


I've tried all of these:






    (new Chunk(world, x, y)).setChunkLoaded(false);


However none of them seem to affect the cache size, which goes relentlessly upwards until a malloc error ends its life.


What is the correct way to flag these cached chunks for unloading?



Link to comment
Share on other sites

ChunkProviderServer.unloadAllChunks.. This appears to unload all the chunks... I wonder how it does that..

    public void unloadAllChunks()
        Iterator iterator = this.loadedChunks.iterator();

        while (iterator.hasNext())
            Chunk chunk = (Chunk)iterator.next();
            this.dropChunk(chunk.xPosition, chunk.zPosition);

It loops through all loaded chunks.. can calls dropChunk....

But I only want to unload one chunk... I wonder what should I do?

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Link to comment
Share on other sites

Thanks for this; I'd found IChunkProvider but not ChunkProvderServer.  Obviously now I can call dropChunk() to get rid on the individual chunks (for some reason, dropChunk does not appear to exist in IChunkProvider).


I assume that the instance of ChunkProviderServer to use this one --

ChunkProviderServer cps;
cps = MinecraftServer.getServer().worldServers[0].theChunkProviderServer;
cps.saveChunks(true,null); // make sure we have no pending updates


... and indeed, the getLoadedChunkCount() from both the IChunkProvider and the ChunkProviderServer now never goes above approx 700, and the memory death is averted.  ;D


Thanks for the help


Link to comment
Share on other sites

ChunkProviderServer cps;
cps = MinecraftServer.getServer().worldServers[0].theChunkProviderServer;


So, you only need that for the overworld then?

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

By using this site, you agree to our Terms of Use.