Supplementaries

Supplementaries

82M Downloads

[1.18.2] Memory leak in CapturedMobCache

embeddedt opened this issue ยท 4 comments

commented

I discovered this on 1.18.2 with mod version 1.5.14 but it looks like 1.19 would also be affected based on the code. The entities created and stored by this class can retain a reference to the client world even after the player disconnects/changes dimension, which causes a memory leak.

In 1.18.2 the PEDESTAL_CRYSTAL field in particular never appears to be reset which causes a permanent leak of the first loaded world.

public static final Lazy<EndCrystal> PEDESTAL_CRYSTAL = Lazy.of(() -> {
EndCrystal entity = new EndCrystal(EntityType.END_CRYSTAL, Minecraft.getInstance().level);
entity.setShowBottom(false);
return entity;
});

A simple fix for these issues would be to reset this cache whenever the client world changes, or at least to change the world which the entity is in.

commented

Ah shit that's bad. I was really careful when dealing with this stuff on another similar thing I had where I did take care of this but guess I forgot it here. Clear on world unload will solve

commented

so actually this was only in 1.18. In 1.19 i dont have that hard reference as a static field and all entities are stored in a guava cache which will discard them when they arent accessed and as soon as that happens the level will be cleared too

commented

The Guava cache only discards after a minute of no access. If the entity is accessed frequently enough, it will retain the old world reference forever (as from what I can tell, the world is never updated once the entity is created). Additionally, this retention policy means that players with low RAM might have trouble switching worlds, as the old world is never discarded before a minute passes.

I would recommend simply clearing all caches on world unload in both 1.18 and 1.19, there should be no visible performance impact from doing so.

commented

It does it does. Too bad I figured it out right after I uploaded that while I was checking 1.19 where I did infect do that. Might release another one later