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.
What is it? Why does it keep reference the sprite?
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!
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.
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
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?
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
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?
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?
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