(1.12) Memory Leak: EntityDatabase#entryMap
Meldexun opened this issue ยท 3 comments
Describe the bug
The EntityDatabase#entryMap
map stores LivingEntityData
as values which have a reference to their entity instance. This map should be cleared in EntityDatabase#updateClient()
but I think it would be better to clear the map with an IWorldEventListener#onEntityRemoved(Entity)
and the PlayerLoggedOutEvent
. Otherwise while in the main menu a WorldClient
will still be loaded in memory.
To Reproduce
Steps to reproduce the behavior:
- Join a world
- Leave the world
- See
WorldClient
instance not being garbage collected
Context (please complete the following information):
- Minecraft version: 1.12.2
- Forge version: 14.23.5.2855
- Mo' Bends version: 1.1.0
Finally got around to this issue, thanks for the report. Learned about weak references recently, and about WeakHashMap. I think this should eliminate the issue entirely. What do you think?
WeakHashMap:
"Hash table based implementation of the Map interface, with weak keys."
(https://docs.oracle.com/javase/8/docs/api/java/util/WeakHashMap.html)
So you would need to change it to use the entity as the key. Then this should work fine.
Edit: Keep in mind that the values of a WeakHashMap are held by strong references. This means the entries of the map won't be cleared if the value prevent the keys from being garbage collected. This is the case here since LivingEntityData holds a reference to the Entity object.