Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minor memory leaks #258

Merged
merged 4 commits into from
Jun 6, 2022
Merged

Conversation

embeddedt
Copy link

@embeddedt embeddedt commented Jun 5, 2022

ItemMobSpawner uses a hash map to cache entity instances for rendering them inside spawners. These entities hold a reference to a client world in order to render correctly. However, based on reading that code, that reference only appears to be updated when the spawners are visible in NEI. This leads to the spawners retaining an extra, unnecessary WorldClient instance when the player changes dimensions.

I've fixed this problem by simply removing and recreating the entities if the client world changes.

NEIController keeps a manager instance for a GUI even after it's closed. If the player then switches dimensions, the previous world is not able to be garbage collected till the GUI is reopened. This is fixed by setting manager back to null once the GUI is closed.

@Dream-Master Dream-Master requested review from mitchej123 and a team June 5, 2022 16:21
@eigenraven
Copy link
Member

Can you use a WeakHashMap instead and lazily initialize the entities when they go missing? That way you won't have to rely on calling the cleanup function at the right time

@embeddedt
Copy link
Author

Good idea. Thanks for that suggestion, in the process of verifying it worked I actually managed to find two more leaks: one in vanilla and another in NEI (fix included here).

@embeddedt embeddedt changed the title Fix memory leak caused by mob spawner items Fix minor memory leaks Jun 5, 2022
@eigenraven
Copy link
Member

Ah sorry, now looking through it I'm not sure a weak map is the best solution. The weak map is the only thing that ever holds references to these entities, as spawnEntityInWorld is never called, so they will be collected on every gc cycle, causing wasteful entity re-creating. The world object on these entities seems to be reset in the renderer class, maybe just set the world object to null after the constructors are run, and add setting it to null to SpawnerRenderer after the rendering is done?

@embeddedt
Copy link
Author

embeddedt commented Jun 5, 2022

I believe the entities are referenced by the world that contains them (the WorldClient, within loadedEntityList) and that is referenced by the Minecraft client class, therefore it shouldn't be possible for them to be collected until the player switches worlds.

@eigenraven
Copy link
Member

I believe the entities are referenced by the world that contains them (the WorldClient, within loadedEntityList) and that is referenced by the Minecraft client class, therefore it shouldn't be possible for them to be collected until the player switches worlds.

The problem is that they're not added to that list - the Entity constructor does not put the entity in the loaded list. Only when the World.spawnEntityInWorld function is called that entity would be added there - but these are not "real" entities so NEI doesn't call that function (rightfully)

@embeddedt
Copy link
Author

Interesting, I am pretty sure I saw them in loadedEntityList when I was investigating the heap dump, but perhaps I mixed them up with something else.

Should I just revert to my original solution then? I'm not sure how mods will react to the world object on the entity being null.

@eigenraven
Copy link
Member

Yeah, the original should be ok

@embeddedt
Copy link
Author

Updated.

@Dream-Master Dream-Master merged commit de2b2ec into GTNewHorizons:master Jun 6, 2022
Glease added a commit that referenced this pull request Jan 10, 2025
#258 addresses the memory leak in ItemMobSpawner. Unfortunately the clean up handler is not consistently called on every world unloaded. This PR fixes this issue by moving the call out of that if block
ChrisJStone pushed a commit to PFAA-Updates/NotEnoughItemsGTNH that referenced this pull request Jan 10, 2025
GTNewHorizons#258 addresses the memory leak in ItemMobSpawner. Unfortunately the clean up handler is not consistently called on every world unloaded. This PR fixes this issue by moving the call out of that if block

(cherry picked from commit 31173a1)
boubou19 pushed a commit that referenced this pull request Feb 4, 2025
#258 addresses the memory leak in ItemMobSpawner. Unfortunately the clean up handler is not consistently called on every world unloaded. This PR fixes this issue by moving the call out of that if block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants