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

Textures/Mats don't unload from memory if I load a new texture before unloading previous one.

Discussion in 'Addressables' started by mwfpg, Nov 18, 2019.

  1. mwfpg

    mwfpg

    Joined:
    Nov 18, 2019
    Posts:
    2
    In a sample texture swapping project I created, the textures don't unload in memory (checked with build) if I load a different texture first. Here is the code where I release and then load a new texture, which causes a delay where the material has no texture:

    Code (CSharp):
    1.     private IEnumerator SetTextureInternal(int texIndex)
    2.     {
    3.         if (_currentTextureHandle.IsValid())
    4.         {
    5.             Addressables.Release(_currentTextureHandle);
    6.         }
    7.  
    8.         var textureReference = _textureReferences[texIndex];
    9.         _currentTextureHandle = textureReference.LoadAssetAsync<Texture2D>();
    10.  
    11.         yield return _currentTextureHandle;
    12.         _skinMaterial.mainTexture = _currentTextureHandle.Result;
    13.     }
    This code above works and the previous texture is unloaded from memory.

    However, I want the swap to be seamless, so when I change it to load a new texture first and then release the previous one, the texture stays in memory even though there is no reference to it.

    Code (CSharp):
    1.     private IEnumerator SetTextureInternal(int texIndex)
    2.     {
    3.         if (_currentTextureHandle.IsValid())
    4.         {
    5.             _pastTextureHandle = _currentTextureHandle;
    6.         }
    7.  
    8.         var textureReference = _textureReferences[texIndex];
    9.         _currentTextureHandle = textureReference.LoadAssetAsync<Texture2D>();
    10.  
    11.         yield return _currentTextureHandle;
    12.         _skinMaterial.mainTexture = _currentTextureHandle.Result;
    13.  
    14.         if (_pastTextureHandle.IsValid())
    15.         {
    16.             Addressables.Release(_pastTextureHandle);
    17.         }
    18.     }
    Is this a bug? I'm not sure what I'm doing wrong or what is causing the reference to stick around in memory. Calling Resources.UnloadUnusedAssets doesn't clear it from memory either. According to the Addressables profiler the counter for the past addressable was released correctly as well.
     
  2. Lukas-Labaj

    Lukas-Labaj

    Joined:
    Nov 22, 2012
    Posts:
    36
  3. mwfpg

    mwfpg

    Joined:
    Nov 18, 2019
    Posts:
    2
    I'm working in 1.3.8, and I don't see any references to the textures in the profiler, even though they are still loaded. I don't think this is the same bug.