Search Unity

Assets in memory after unloading a scene.

Discussion in 'Scripting' started by _Yash_, May 11, 2018.

  1. _Yash_

    _Yash_

    Joined:
    Oct 17, 2014
    Posts:
    6
    Hi, While investigating a memory leak I came across a strange thing.
    I need to understand what exactly is happening when the scene is unloaded and what, why and how objects are kept in the memory.

    Scenario:
    I have one scene(1) which additively loads another scene(2) this loader scene(1) do not refer to many assets, 2-3 textures max. Now the scene(2) which was additively loaded gets unloaded, Resources.UnloadUnusedAssets is called, GC.Collect() is called. after that, if I take a detailed memory snapshot in profiler, it shows assets which were referenced from the scene(2) which is already unloaded, how is this possible?

    I have checked for hidden objects but there are none which refer to this assets. Profiler does collect the references to assets in which it shows "ManagedStaticReference()", what this refers to?

    Also, I have questions about references of Monobehaviour, if an object (which is not a monobehaviour) holds a reference of a monobehaviour, is it possible that this monobehavour will live even if the gameObject on which MonoBehaviour is attached is destroyed?

    Using Unity5.3.8f1
     
  2. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Are you testing this in your build or in the editor?

    Because the editor will keep objects in memory longer, you really should use a build for this. A ManagedStaticReference means you have some script that still refers to this object, that is being kept alive because of a static property. For example, its very important when using Singletons to nullify the static Instance reference OnDestroy, otherwise Unity can never unload those references.

    You can see this with the mono memory profiler https://bitbucket.org/Unity-Technologies/memoryprofiler/src, but you need il2cpp for older versions i think.
     
    _Yash_ likes this.
  3. _Yash_

    _Yash_

    Joined:
    Oct 17, 2014
    Posts:
    6
    Thanks, TJHeuvel-net
    Yes, I saw UnityEditor references while profiling in the editor. I also checked the same on UnityPlayer on windows which still holds references to assets which are from the already unloaded scene.
    Right now I am stuck with Unity5.3.8f1 so the profiler you have mentioned cannot be used but I can temporarily check the project on a newer version of unity.
    Is there any other way to investigate memory leak with mono2x ?
     
  4. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    Not really, sadly. It does say Unity 5.3a4, arent you higher than that?

    Otherwise the only real thing you can do is search for any static references that might not be set to null after your scene unloads.
     
  5. _Yash_

    _Yash_

    Joined:
    Oct 17, 2014
    Posts:
    6
    5.3a4 is for IL2CPP and I am targeting windows standalone so no IL2CPP support. And for .NET 3.5 I have to use 2017.3.0b1 as per the doc.
    Thank you for the response, will check static references.
     
  6. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
    Last edited: Nov 25, 2020
    funkyCoty and _Yash_ like this.