SparklyPower / SparklyPaper

✨ "Ooooh, sparkly paper!" - SparklyPower's Paper fork, making large servers snappier with high-performance optimizations and improvements!
https://sparklypower.net/
120 stars 13 forks source link

Thread memory leak #39

Closed jtJava closed 1 month ago

jtJava commented 1 month ago

Long story short I applied the parallel world ticking patch onto AdvancedSlimePaper, and discovered a memory leak which is only noticed when loading/unloading worlds.

I tested this memory leak on the latest release of SparklyPaper with the following code and attached plugin.

I tried fixing this myself but wasn't able to, otherwise I'd be open to making a pull request or just fixing it in my own fork.

public final class SparklyMemoryLeak extends JavaPlugin {
    @Override
    public void onEnable() {
        Bukkit.getServer().getScheduler().runTaskTimer(this, () -> {
            World world = Bukkit.createWorld(new WorldCreator(UUID.randomUUID().toString()));
            if (world != null) {
                Bukkit.getServer().getScheduler().runTaskLater(this, () -> {
                    Bukkit.unloadWorld(world, false);
                }, 20);
            }
        }, 20, 20);
    }
}

Below is an image of what a heap dump will look like after running the following code for a few minutes. image

MrPowerGamerBR commented 1 month ago

What needs to be checked is if ServerLevel.close() is never called by the server, because that's where the thread executor should be closed.

HOWEVER it looks like it is never called by the server, so I think I need to move it to close the thread executor somewhere else.

MrPowerGamerBR commented 1 month ago

btw I do not recommend using the JARs in the releases tab, they are only "JARs that I tested on my public server and they work fine"

MrPowerGamerBR commented 1 month ago

Fixed in https://github.com/SparklyPower/SparklyPaper/commit/0fd6b8d393236908e984ad093571772373c70e1b

jtJava commented 1 month ago

Thanks, I thought that might have been the issue but wasn’t sure where to move it. I will test and let you know when I have time