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

[Zenject SampleProject Issue] Object that has Reference to the SceneContext stay in memory after scene unloaded #246

Open
sapiscow opened this issue Jul 7, 2021 · 2 comments

Comments

@sapiscow
Copy link

sapiscow commented Jul 7, 2021

Hello!

I've found that Zenject somehow is "leaking" in memory, i used the advanced project example from Zenject. Any instances that has bound to SceneContext are kept in memory even when the scene has been unloaded such as the GameInstaller and anything that used Pool. Any help to solve this issue (or find a workaround for it) would be appreciated!

Steps to reproduce:

  • Use the example project from Zenject (SpaceFighter)
  • Then, I just adding an empty scene with only one gameobject that contains default SceneContext, LoaderScript to load back the SpaceFighter scene, and Zenject Binding with that loader script component
  • After that i do profilling from memory profile. What i do is just from SpaceFighter i go to empty scene, back to SpaceFighter scene, back again to empty scene for a few times
  • Memory Profiler shows the instance of the object that i've mentioned above is increasing as much as i load the scene

Sample Project to reproduce:
Zenject Test.zip

@sapiscow sapiscow changed the title Object that has Reference to the SceneContext stay in memory after scene unloaded [Zenject SampleProject Issue] Object that has Reference to the SceneContext stay in memory after scene unloaded Jul 7, 2021
@johnfriedrich
Copy link

What editor version? We are getting an infinite Application.Reload issue after the second time entering playmode with Unity 2020.3 with our project. 2019.4 is fine. Mabe it has the same root cause

@sapiscow
Copy link
Author

@johnfriedrich Unity 2020.3.12f1, i've fixed this issue with adding:

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

in SceneContext.cs of Zenject.

And, i also need to remove all reference manually when i injected a reference in a class (this is taking too much time).

villetuh added a commit to villetuh/Extenject that referenced this issue Dec 28, 2022
Zenject doesn't properly clear reference from scene container when scene
is unloaded. This results in memory leak when loading and unloading
scenes.

Fixed the issue by unbinding all reference from scene container when
SceneContext component is destroyed.

modesttree#246
Mathijs-Bakker pushed a commit to Mathijs-Bakker/Extenject that referenced this issue Jan 8, 2023
Zenject doesn't properly clear reference from scene container when scene
is unloaded. This results in memory leak when loading and unloading
scenes.

Fixed the issue by unbinding all reference from scene container when
SceneContext component is destroyed.

modesttree#246
sadegh-askari pushed a commit to sadegh-askari/Extenject that referenced this issue Jun 29, 2024
Zenject doesn't properly clear reference from scene container when scene
is unloaded. This results in memory leak when loading and unloading
scenes.

Fixed the issue by unbinding all reference from scene container when
SceneContext component is destroyed.

modesttree#246
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

No branches or pull requests

2 participants