Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Do I need to do any cleanup of Assets loaded from Addressables?

Discussion in 'Addressables' started by chanon81, Oct 25, 2018.

  1. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    I remember seeing something about having to cleanup assets/instances in a special way from a talk.

    Looking at the documentation, I can't seem to find it anywhere:
    https://docs.unity3d.com/Packages/c...4/manual/AddressableAssetsMigrationGuide.html

    Addressables has a ReleaseAsset and a ReleaseInstance method.
    Do I need to manually call them? And when should I?

    Also, I'm still a bit confused as to when to use Addressables.Instantiate and when to use Addressables.LoadAsset and if they are interchangable and what they actually do that is different.
     
  2. redbox9

    redbox9

    Joined:
    Apr 27, 2018
    Posts:
    7
    I believe they work as follows:

    - Addressables.LoadAsset will load an asset into memory for use but won't instantiate it in the scene.
    - Addressables.Instantiate will load an asset into memory and instantiate it in the scene.

    - Addressables.ReleaseInstance will release an instance that was instantiated via Addressables.Instantiate. If you instantiate an object with Addressables.Instantiate but then destroy it using Object.Destroy, you bypass the addressables system and it will still hold the reference to the instance.

    I believe you can replace all instances of Object.Destroy with Addressables.ReleaseInstance. If the object was instantiated via addressables it will be handled correctly, if it was instantiated outside of addressables it will simply be destroyed.

    - Addressables.ReleaseAsset should be used to release an asset that was loaded via Addressables.LoadAsset.
     
  3. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    Thank you!

    So I have to manually keep track of everything I created with the Addressable system?

    Is there a standard way to do this in Unity/C#? (I'm new to Unity.)
    For example I see there is an OnDestroy callback on MonoBehavior that maybe I could use? However it says that it is called when the Scene is destroyed. What about when the owner GameObject is destroyed?

    Eg. maybe my MonoBehavior used Addressable to instantiate a new child to it's gameObject. Then when the gameObject gets destroyed, my MonoBehavior should release the child instantiated with Addressable. How would I do that?

    Also, what about Adressable assets used on normal built-in MonoBehaviours such as a Sprite on a SpriteRenderer that I dragged to assign in the inspector?
     
  4. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    Another question, how can I view all Addressable resources that are currently loaded so that I can check for leaks?
     
  5. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    I implemented my own resource manager / object pooling system that keeps track of what needs to be released back to Addressables.

    I would still like a way to view currently loaded Addressable resources though to be able to be sure I am not leaking.
     
  6. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    ok, a lot of questions here and I'll see if I can get to them..

    - yes you need load/release pairs, except in the scenario of items in a closed scene. If you do an instantiate, then close the scene, we will see the objects in their are getting destroyed and will handle it.

    - to the question of keeping up with this, generally yes. One note is that you can replace every call of GameObject.Destroy in your game with Addressables.ReleaseInstance(). If the thing was not created with addressables, we'll just fall back to doing a GameObject.Destroy for you.

    - you mention wanting a callback on game objects being destroyed. If they were destroyed because the scene was closed, then we're covered by my answer above. If they were destroyed some other way, then hopefully you can just be doing a ReleaseInstance

    - checking for leaks... open Window->Asset Management->Addressable Profiler and make sure that "Send Profile Events" is enabled in your main addressable assets settings object.
     
    rioneye likes this.
  7. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    Thank you for the responses!

    In the Addressable Profiler, how would I know there are leaks? I can see the events happening, but there doesn't seem to be like a "reference count left" for each asset to view.
     
  8. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    the green (I think it's green, if I remember right) part of the chart shows ref count. This may only work if you are doing Virtual mode when entering play mode, as this will pretend their are asset bundles, and ref-count those.
     
  9. KasperSP

    KasperSP

    Joined:
    Mar 7, 2017
    Posts:
    4
    Is there a way to check if an addressable asset has already been loaded? Or maybe the addressable system is aware of this and avoids loading something that is already loaded?
     
  10. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    @KasperSP If i understood it correctly as long as you use the methods from Addressables.* it is aware of it, however if you instantiate anything via Object.Instantiate etc you have to manage reference on your own. It seems therefore to be recommended to move completly to the addressable system (loading, instantiation, destruction).
     
  11. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    yes, you can call LoadAsset() twice, and you'll get a ref count of 2, but won't actually re-load the thing. You'll need two ReleaseAsset() calls to get the ref count back to 0.

    also true. The main reason someone may want to do Addressables.LoadAsset() then save that asset and use Unity's direct Instantiation on the thing is that they can then instantiate synchronously.
     
    chanon81 and MNNoxMortem like this.
  12. Lukas-Labaj

    Lukas-Labaj

    Joined:
    Nov 22, 2012
    Posts:
    36
    Hi,

    In docs for memory management there is one section about unloading partially unload asset:

    This case doesnt work for me. I cant unload only just one asset from memory. Even, Im making mirroring load-unload operation there is still reference to asset (in my case im trying load/unload Sprite). In detailed memory profile i got this ref (see screenshot).

    Am I doing something wrong?

    Loading two different sprites from same bundle:
    Code (CSharp):
    1.  
    2. Addressables.LoadAssetAsync<Sprite>("Assets/Sprites/A/element_fire.png").Completed += handle =>
    3. {
    4.       _opCache.Add(handle);
    5.       image.sprite = handle.Result;
    6. };
    7.  
    8. Addressables.LoadAssetAsync<Sprite>("Assets/Sprites/B/element_fire_1.png").Completed += handle =>
    9. {
    10.     image2.sprite = handle.Result;
    11. };
    12.  
    Trying unload only one sprite:
    Code (CSharp):
    1.  
    2.            foreach (var sprite in _opCache)
    3.            {
    4.                 Addressables.Release(sprite);    
    5.             }
    6.            
    7.             _opCache.Clear();
    8.                            
    9.             image.sprite = null;
    10.  
    11.             Resources.UnloadUnusedAssets();
    12.  
     

    Attached Files:

    ponasoft likes this.
  13. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    Destroy(gameobject) will delete the game object and its children. I am not seeing the same behavior with Addressables.ReleaseInstance. Say i have built up a structure of Addressable instantiated gameobjects do i have to ReleaseInstance of every one vs just the top level game object?
     
    anisimovdev likes this.
  14. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,123
    This is a very good question and I have not found any documentation that makes this clear. Its common in real games that prefabs be instantiated and children to one another. Do we just release instance on the root?
     
    daxiongmao likes this.
  15. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,068
    Excuse me.
    Where is the answer to the above question ?
     
    CSaratakij likes this.