Moulberry / AxiomPaperPlugin

Serverside component for Axiom
MIT License
61 stars 28 forks source link

World unload memory leak #8

Closed LaserSlime closed 6 months ago

LaserSlime commented 6 months ago

This plugin keeps references to worlds even after they are unloaded. This prevents the garbage collection from removing them properly, causing memory leaks in the long run.

Screenshot 2024-05-09 230329

From what I can tell you've already tried to prevent this by using WeakHashMap, but it's not working for some reason.

Feniksovich commented 6 months ago

In the latest implementation of the ServerWorldPropertiesRegistry world references are no longer stored in the WeakHashMap, but now holds a strong reference to a world which still prevents the world object from being removed by the GC. https://github.com/Moulberry/AxiomPaperPlugin/blob/cd8b5c534607cc59fd93425795741d106fde398f/src/main/java/com/moulberry/axiom/world_properties/server/ServerWorldPropertiesRegistry.java#L22 I guess it should be replaced with WeakReference<World> (or Reference<World> like in Bukkit location class).

private final WeakReference<World> world;

The only place where the ServerWorldPropertiesRegistry object is stored is the WeakHashMap: https://github.com/Moulberry/AxiomPaperPlugin/blob/cd8b5c534607cc59fd93425795741d106fde398f/src/main/java/com/moulberry/axiom/AxiomPaper.java#L327 But the value instance still holds strong reference to the world object equals the key, so neither World nor ServerWorldPropertiesRegistry instances won't be garbage collected.

Moulberry commented 6 months ago

Fixed by 26eb24f2084528a1b9dbd8daeea193cf2d2ec161