References bound to a Scene Context stay in memory after scene change
See original GitHub issueHello!
I’ve found that Zenject somehow is “leaking” assets in memory. What I mean by this is that assets that are bound to a Scene Context are kept in memory even when the Scene has been changed. I have the theory that some static reference is keeping those assets from being collected by the GC, but that’s all I got; I couldn’t pinpoint the exact cause of the problem.
Any help to solve this issue (or find a workaround for it) would be appreciated!
Setup to reproduce:
- An empty Scene A.
- A Scene B with a Scene Context, a MonoInstaller and a GameObject with a Component of type
TestInjectableComponent
. TheTestInjectableComponent
should have some references to some assets, such asSprites
(e.g. aList<Sprite>
). The MonoInstaller binds classTestInjectableComponent
to that instance. - A Scene C that only has the GameObject with a
TestInjectableComponent
(with the references to the assets). No Zenject-related components should exist here.
Steps to reproduce: Note: to verify Assets in memory, use Unity’s profiler to take a Memory Snapshot.
- Start in Scene A. Check that no Sprites are loaded into memory.
- Load Scene C (No Zenject). Check that the Sprites referenced by TestInjectableComponent are loaded into memory.
- Load Scene A. Check that no Sprites are loaded into memory.
- Load Scene B (Zenject). Check that the Sprites referenced by TestInjectableComponent are loaded into memory.
- Load Scene A. No sprites should be loaded in memory at this point. However, you’ll find that the sprites are still there.
Sample Project I’ve attached a test project where you can verify this behavior. Use the keyboard to change to the desired scene: S for the “Starting Scene”, N for the “No-Zenject Scene” and Z for the “Zenject Scene”.
Issue Analytics
- State:
- Created 4 years ago
- Comments:11
Top GitHub Comments
@Racso the GameInstaller doesn’t have any reference to another, it just contains Bind functions for any interface that used in the scene.
And now, i’ve tried the sample project from Zenject, there are two sample projects, one that simple and one advance. For the simple project, there are no leak issue, But when i tried the advanced sample, there are same leak issue there I’ve post the issue here https://github.com/modesttree/Zenject/issues/246 I hope Zenject Developer is trying to fix this
Thank you btw @Racso for your help
That being said, I suspect you have other kind of issue. My last suggestion probably won’t make a difference because if your GameInstaller is being unloaded correctly (as you mentioned), it won’t keep references alive in memory (therefore, nullifying the pointers is useless).
Maybe you have statics in your code, or something similar?