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. Dismiss Notice

Addressables: how to truly unload assets to free memory

Discussion in 'Addressables' started by Jarko89, Sep 11, 2020.

  1. Jarko89

    Jarko89

    Joined:
    Mar 8, 2017
    Posts:
    15
    Hi all!

    I have a large world and a system that spawns game objects (from now on: "decorations") and terrains around the player according to the position of the player.

    Previously all existing terrains prefabs were assigned to a scriptable, while decoration prefabs were assigned to a different scriptable. Both scriptables were loaded with Resources.Load soon after the launch of the game. This caused all the prefabs and their linked assets to be loaded into the memory at once, leading to high ram usage.

    Now all these prefabs have become addressables and have been added to 2 different addressable groups, while the scriptables contain a list of AssetReference instead of a list of GameObject. When the system must spawn a decoration or a terrain, it loads the asset reference into memory with Addressables.LoadAssetAsync, then spawns the GameObject with GameObject.Instantiate. If an AsyncOperationHandle for the same asset already exists, the system uses the loaded asset (if ready) or enqueues the spawn until it's ready, so there can't be more than 1 handle for the same asset.

    If all the game objects generated from the same asset have been destroyed, the system releases the handle with Addressables.Release.

    The system works well - initial memory usage is much, much lower. Sadly, it grows slowly over time when the player moves around, as if the assets no longer needed and unloaded where actually never removed from the memory.

    What is causing that? Is it not enough to destroy all the gameobjects and call Addressables.Release on the handle? Or is it the fact that all the decorations / terrains belong to the same group, thus the same asset bundle, and an asset bundle can be partially loaded but not partially unloaded?

    If the reason is the latter, what's the right approach? Split the 2 bundles into multiple bundles based on the region of the world in which the assets are needed?

    Thank you!
     
  2. Jarko89

    Jarko89

    Joined:
    Mar 8, 2017
    Posts:
    15
    Bump. Tried adding labels according to the area of the world where assets are used and then "bake together by label", which generates multiple bundles, but no luck.
     
  3. razamgar

    razamgar

    Joined:
    Apr 7, 2020
    Posts:
    14
    Yes, to the best of my knowledge, asset bundles are unloaded as soon as all its contents have zero references, no sooner than that. You might want to make smaller bundles or even pack each object separately.
     
    MrCool92 likes this.
  4. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,151
    I saw somewhere else a dev said exactly what you asked. Assets are loaded in a partial way as needed, however once you release them, they are only released from memory once the entire set of assets from the bundle are released and have a ref count of 0.