Search Unity

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:
    47
    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:
    47
    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:
    47
    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:
    47
    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    362
    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.
     
  7. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    47
    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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    362
    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:
    3
    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:
    320
    @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

    Unity Technologies

    Joined:
    Apr 11, 2017
    Posts:
    362
    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.