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

add note on unloading dynamically generated meshes #402

Closed
wants to merge 1 commit into from

Conversation

Bersaelor
Copy link
Contributor

I noticed some major memory leaks after loading and unloading many small gltf's in our Unity app.
Our app is basically a browser for gltf files stored in the backend.

After adding the mentioned lines, I was able to keep the memory usage to the gltf files currently shown in the scene, which is especially important for cheaper android devices where memory isn't as big.

Since I spent a while figuring out what had happened, I thought it maybe helpful for other people importing meshes at runtime?

@atteneder
Copy link
Owner

Hi @Bersaelor ,

Thanks a lot for sharing this!

I'm curious, did you try GltfImport.Dispose and did it work for you?

The proposed solution, while working and generic, might have downsides:

  • Accessing .mesh after destroying .sharedMesh (which is not done immediately) might trigger cloning the sharedMesh again.
  • GetComponentsInChildren is not best practice (slow)

I'd suggest to create a more specific solution to the problem. I think GltfImport.Dispose ultimately should handle this.

@Bersaelor
Copy link
Contributor Author

I'd suggest to create a more specific solution to the problem. I think GltfImport.Dispose ultimately should handle this.

Mhmm, I'm not sure how I can use that, I thought the GltfImport is only used to import the asset and not needed anymore afterwards? I.e. after running gltf.InstantiateMainScene(transform) I don't need to hold a reference to the GltfImport class anymore?

So, I'll try keeping a reference to my GltfImport around and use GltfImport.Dispose() when I want to remove the 3D asset from the scene and check the memory imprint.

@Bersaelor
Copy link
Contributor Author

Alright, replaced the destroying of the meshes with a reference

        private GltfImport? importer;

and when unloading I now call Dispose()

        public void unloadContent()
        {
            if (!isLoaded)
                return;

            if (importer != null)
                importer.Dispose();

            foreach (Transform child in transform)
                GameObject.Destroy(child.gameObject);
        }

which works. Thank you @atteneder . Maybe instead of the proposed PR note, we should put a note in the documentation reminding people to use importer.Dispose()? if we use a custom method with gltfImport or is this something more experienced Unity devs would assume naturally?

@atteneder
Copy link
Owner

I made this paragraph now, which makes this PR obsolete.

Thanks for bringing it up!

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.

2 participants