[1.18.2] Memory leak in CapturedMobCache
embeddedt opened this issue ยท 4 comments
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.
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.
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
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
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.