Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question ManagedStaticReferences when unload unused resources

Discussion in 'Scripting' started by lehuynhthang99, Dec 26, 2020.

  1. lehuynhthang99

    lehuynhthang99

    Joined:
    Sep 8, 2020
    Posts:
    9
    I have some assets (sprite) need unload after changing scene async. After changing scene, the assets are still there and the only reference is ManagedStaticReferences.

    173071-screenshot-2020-12-25-164543.png

    And this is the snapshot from the memory profiler (click on the ref count):

    173072-screenshot-2020-12-25-164916.png The question are:
    1. What is it? Why does it keep reference the sprite?
    2. How can I track those ref?

    Btw, can I use Weak Ref to store loaded asset? I did use it in Unity 2019.2 and it run perfect with Resources.UnloadUnused but is it ok? Thanks for your concern!

    Note: I use Unity 2019.4.16f1 now
     
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,164
    You can ignore that reference, it's an internal thing that is basically the same as no reference. Also you might want to update your Memory Profiler package to get a cleaner view on this. Not that 0.2.7-peview.1 removes the ManagedStaticRoot global thingy yet, I think that change is till pending, but it does clarify the table UI somewhat, especially the sizes.

    Asides from that: how do you unload your scene, i.e. with which Async command and options? Are you calling GC.Collect and Resources.UnloadUnusedObjects before or after loading the next scene?

    Yes that should be fine, just remember that UnityEngine.Object inheriting types implement a fake null so the WeakPointer to the managed Sprite type might still be valid while the Native Object underneath it is already gone and
    == null
    will return false.
     
    lehuynhthang99 likes this.
  3. lehuynhthang99

    lehuynhthang99

    Joined:
    Sep 8, 2020
    Posts:
    9
    This is the process I use, hope it explain well:
    Tmp.png
    I use async to load scene additive and just use Resource.UnloadUnused (not GC.Collect). The same for unload scene. I use coroutine to wait for the process to complete. Snapshot 2 still have some resource in SceneA just like the images I show in the post. And the resource that I need to unload is still in the unload bundle cause I just want to unload some of them which don't use, not all. Hope it's not the reason in my case

    Note: Forgot to mention, the bold text is the active scene

    Does that mean it doesn't increase ref count? And the null part you mention, instead of calling if (obj == null), call if (obj) is more precise, doesn't it?

    Btw, how can I track the Unity part in the memory profiler (the standard one, not the memory profiler package)? I profile the memory on the mobile and Unity part sometime take a lot of memory
     
    Last edited: Dec 26, 2020
  4. lehuynhthang99

    lehuynhthang99

    Joined:
    Sep 8, 2020
    Posts:
    9
    Last edited: Dec 28, 2020
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,164
    I assume that is the AssetBundle containing the assets (and scene?) From scene A?

    Are you quite sure that at step 4, before you call Resources.UnloadUnusedAssets, nothing references the asset bundle anymore? Did you try taking a snapshot just before calling UnloadUnusedAssets? You can use this API to take the snapshot just before that, store it on devices and download it.

    If you call GC.Collect() and GC.WatForPendingFinalizers() before calling UnloadUnusedAssets, does that make the AB get unloaded properly?
     
  6. lehuynhthang99

    lehuynhthang99

    Joined:
    Sep 8, 2020
    Posts:
    9
    Yup, I use 1 asset bundle to store some UI assets. And not only scene A, I use those asset for scene B and C as well

    That's why I stiil reference the asset bundle in my code cause I thought I can unload a part of loaded asset using Resources.UnloadUnusedAssets

    Untitled Diagram.png
    As I understand from your explanation, if I don't use the pt.1 anymore but still ref the AB, I can't unload this part when using Resources.UnloadUnusedAssets, right? So the only choice to unload this part is to unload all the bundle?
     
  7. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,164
    I have to dig into AssetBundles a bit deeper myself in the new year but yes, that is my current understanding of how they work in memory. If you have a part that can be unloaded independently from the other part, maybe it's worth splitting the bundle?
     
    lehuynhthang99 likes this.
  8. lehuynhthang99

    lehuynhthang99

    Joined:
    Sep 8, 2020
    Posts:
    9
    Thanks a lot for your reply. I've been wandering around this problem these days and got no where else to ask. Save my day =)))
     
    MartinTilo likes this.
  9. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,164
    actually, I double checked the Addressables Documenation and according to that:
    So with the caveat of me still not being too firm on how the Addressables system handles memory, in line with this thread:
     
    lehuynhthang99 likes this.
  10. lehuynhthang99

    lehuynhthang99

    Joined:
    Sep 8, 2020
    Posts:
    9
    I will. Hope they make it more clear in the doc about how the asset bundle, addressable and resource work with each other.
    Anyway, that should work for now
     
    MartinTilo likes this.