Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

Bug Resources.UnloadUnusedAssets cannot unload assets without any references.

Discussion in 'Addressables' started by zhuxianzhi, Apr 6, 2021.

  1. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    85
    Code (CSharp):
    1.  private void Update()
    2.     {
    3.         if (Input.GetKeyDown(KeyCode.K))
    4.         {
    5.             a = Addressables.LoadAssetAsync<TextAsset>("masterMemoryDB1");
    6.             b = Addressables.LoadAssetAsync<TextAsset>("masterMemoryDB2");
    7.         }
    8.  
    9.         if (Input.GetKeyDown(KeyCode.R))
    10.         {
    11.             Addressables.Release(a);
    12.             Debug.Log("Release A");
    13.         }
    14.  
    15.         if (Input.GetKeyDown(KeyCode.A))
    16.         {
    17.             Addressables.Release(a.Result);
    18.             Addressables.Release(b.Result);
    19.             Debug.Log("Release A,B");
    20.         }
    21.  
    22.         if (Input.GetKeyDown(KeyCode.U))
    23.         {
    24.             _asyncOperation = Resources.UnloadUnusedAssets();
    25.             _asyncOperation.completed += operation => { Debug.Log("UnloadUnusedAssets complete"); };
    26.         }
    27.     }
    When I press K key to load asset A and B, press R key to unload asset a, and the profiler shows that asset a has no references. Call Resources.UnloadUnusedAssets() again, asset A cannot be unloaded.

    If I uninstall A and B, the asset can be uninstalled normally.

    https://docs.unity3d.com/Packages/c.../MemoryManagement.html#when-is-memory-cleared


    QQ截图20210406111724.png


    addressables 1.16.16
    Unity 2020.3.0f1
     
    Last edited: Apr 6, 2021
  2. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,373
    If A and B are in the same bundle, that is expected. We're working on getting better connection tracking between AssetBundles and the Assets they loaded in into the memory Profiler backend. As you noticed, this connection is currently not reported to the Profiler, making it seem as though there would be no reference to it.
     
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,373
    It's also described in the Documentation page you linked to:

     
  4. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    85
    Thanks Reply.

    AssetBundle can achieve more detailed memory management. Why doesn't Addressables.Release call Object.Destroy?
    I can only build A and B into two bundles? But this way will cause Serialized File Overhead.

    Code (CSharp):
    1.            
    2.            var ab = AssetBundle.LoadFromFile("Ab");
    3.            var a = ab.LoadAsset<TextAsset>("a");
    4.            var b = ab.LoadAsset<TextAsset>("b");
    5.            Object.Destroy(a);
     
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,373
    I believe it does. The object is gone. If you had multiple instances of it, they'd be all gone but the native memory associated with the asset is still there because it was loaded into memory as part of the bundle.

    Yes, to a degree. Depending on how you load them (i.e. single assets from file or the entire bundle directly into memory) you can control when what gets loaded in, just not that it gets unloaded before the bundle gets unloaded.

    If you need this fine grained control on unloading, you need to split the bundles. The serialized file overhead is something that needs to be part of weighing that decision over.
     
    zhuxianzhi likes this.
  6. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    85

    I read the document carefully

    "You can load an AssetBundle, or its partial contents, but you cannot partially unload an AssetBundle. No Asset in 'stuff' will unload until the bundle itself is completely unloaded. The exception to this rule is the engine interface Resources.UnloadUnusedAssets. Executing this method in the above scenario will cause 'tree' to unload."

    According to my test case, after loading A and B at the same time, only Release A, calling Resources.UnloadUnusedAssets should be able to unload A.

    What did I miss?
     
    FranClip likes this.
  7. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    85
    Can anyone answer my question?

    Thanks in advance
     
  8. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    85
    I use version 1.16.19 and use Resources.UnloadUnusedAssets in the above situation and it works normally.

    Newer versions such as 1.18x will not work properly.

    @MartinTilo @davidla_unity
     
  9. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    1,373
    Hey again,
    according to the devs on addressables, nothing affecting this should have changed but it might be a bug. Could you please file a report through Help -> Report a Bug in the Editor?
     
  10. zhuxianzhi

    zhuxianzhi

    Joined:
    Mar 30, 2015
    Posts:
    85
    Case 1344093
     
    MartinTilo likes this.
unityunity