Fix memory leaks caused by reused arrays of Spatials#2642
Fix memory leaks caused by reused arrays of Spatials#2642parysto wants to merge 1 commit intojMonkeyEngine:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses critical memory leaks in JME3 where Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses two memory leaks related to object pooling and reused arrays. In Spatial.java, the change correctly clears references from a temporary stack and defers releasing the temporary variables object until it's no longer in use. In ListSort.java, the temporary array used for merge sort is now cleared, preventing stale references. Both changes are crucial for preventing memory leaks in long-running applications where scene graph elements are frequently attached and detached. I've also included a suggestion to fix a pre-existing off-by-one error in Spatial.java which could lead to a NullPointerException.
|
Good find! Since this is likely a bug that can potentially present itself everytime that TempVars field is used without proper cleanup, maybe we should fix it right into the TempVars.release method? jmonkeyengine/jme3-core/src/main/java/com/jme3/util/TempVars.java Lines 126 to 142 in f02c4e6 we can call Arrays.fill from there, thoughts? |
|
Yes, calling |
If (multiple) Geometries with Textures are being attached to and detached from the Sceengraph at runtime (in
update(...)), the Texture memory counter increases continuously and does not reach 0 again. This happens especially afterSpatial#getWorldTranslation()has been called.Even after detaching the Geometries' parent node, cleaning the asset-cache and calling
System.gc(), it does not go back to 0.After some debugging, I found out that this is caused by how ref-variables are used to improve performance:
Spatial#checkDoTransformUpdate()uses aSpatial[]fromTempVars, but does not clean up the references.ListSort#sort(...)performs a merge-sort if there are more than 128 entries. The merge sort stores a reference to a temporary array of sorted entries in a class field, but does not clean up the references set in this array after sorting.This means that parts of the detached sub-Sceengraph survive garbage collection. The Texture memory counter just happens to be the most visible indicator...