question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

References bound to a Scene Context stay in memory after scene change

See original GitHub issue

Hello!

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. The TestInjectableComponent should have some references to some assets, such as Sprites (e.g. a List<Sprite>). The MonoInstaller binds class TestInjectableComponent 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”.

Zenject Leak Test.zip

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
sapiscowcommented, Jul 7, 2021

@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

0reactions
Racsocommented, Jul 7, 2021

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

FindGameobject or keep References in memory? Best ...
If you're having to iterate through all scene objects to find a specific one that matches some specific criteria, I'd argue that you're...
Read more >
References bound to a Scene Context stay in memory after scene ...
What I mean by this is that assets that are bound to a Scene Context are kept in memory even when the Scene...
Read more >
How to pass data (and references) between scenes in Unity
See #2 for a workaround. 2.Use the DontDestroyOnLoad function. You only need to use this if the data to keep or pass to...
Read more >
Change Unity Scenes, Keep Data - YouTube
Using Scriptable Objects, you can have data persist between scene ... This data will only survive in memory and won't be permanently saved....
Read more >
Find, Fix, and Avoid Memory Leaks in C# .NET: 8 Best ...
Since they are referenced, the garbage collector won't collect them and they will remain forever, taking up memory.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found