ssannandeji / Zenject-2019

Dependency Injection Framework for Unity3D
MIT License
2.53k stars 366 forks source link

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

Open Racso opened 4 years ago

Racso commented 4 years ago

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:

Steps to reproduce: Note: to verify Assets in memory, use Unity's profiler to take a Memory Snapshot.

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

wirde commented 4 years ago

Did you find any solution/workaround for this? I also have leaks in my project which I believe could be related to this behavior.

koldkane commented 4 years ago

I find ZEN_INTERNAL_NO_POOLS define word can fix the issue in the sample project.

koldkane commented 4 years ago

Here is the fix:

https://github.com/svermeulen/Extenject/pull/64

sapiscow commented 3 years ago

Hello, i've found the same issue as here, have anyone find the solution?

Racso commented 3 years ago

@sapiscow Have you tried using Zenject's current project? This version is obsolete and hasn't been updated in quite a bit. Check https://github.com/modesttree/Zenject for the current version.

sapiscow commented 3 years ago

Yes, i've tried the latest version (Extenject 9.2.0) in Unity 2020 LTS, did you solve your problem with only update the Zenject? or Is Unity 2020 not supported yet?

Racso commented 3 years ago

@sapiscow Not really. We made a workaround ourselves as Zenject wasn't being updated at that time (and we didn't know about Extenject).

It's been a while, and I no longer have easy access to that git history to verify for sure what we did (we tried a lot of stuff). However, I suspect that the key of the fix was adding this in SceneContext.cs:

private void OnDestroy()
{
    _container.UnbindAll();
}

Give it a try.

sapiscow commented 3 years ago

@Racso After i add that code in SceneContext.cs, the GameInstaller (an installer script that also in the same gameobject as SceneContext) is cleared from memory after the scene unloaded, but the others is still have issue

Racso commented 3 years ago

@sapiscow OK, last shot I can give you:

In your installers (e.g. your GameInstaller), add code in OnDestroy to nullify any references you have. Basically, do this:

private void OnDestroy()
{
  referencedObject1 = null;
  referencedObject2 = null;
  referencedObject3 = null;
}

Where rerencedObject1, referencedObject2 and referencedObject3 are objects (e.g. MonoBehaviours) that you referenced in the installer.

For example: if you had this in the installer:

[SerializeField] private MyGameInput input;

You would add this in OnDestroy:

input = null;

Racso commented 3 years ago

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?

sapiscow commented 3 years ago

@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