Search Unity

Resolved Properly disposing of Texture2D and releasing memory

Discussion in 'General Graphics' started by Noctre, Jan 29, 2021.

  1. Noctre

    Noctre

    Joined:
    Dec 30, 2018
    Posts:
    3
    I am currenly trying to do some image processing using shaders and Texture2Ds.

    I quickly noticed a spike in memory usage and after a bit of searching I found out that its caused by Texture2Ds not disposing correctly. I tried to minimize unnecesary allocation by reusing objects as much as possible, but the problem still persisted.

    I managed to (or at least I hope I did) resolve RenderTextures not releasing memory by using RenderTexture.Release() and GL.Clear.

    I tried using Object.Destroy and Resources.UnloadAsset to force Texture2D to release memory, but they would stay loaded (even with no references remaining).

    The only time they would unload and release memory is by calling Resources.UnloadUnusedAssets.
    However it isn't exactly an ideal solution for a subsystem to do a system-wide action implicitly, that is, unloading ALL unused assets.

    Is there any other way of actually unloading just the texture I want?
    So far I heard about Addressables having that kind of functionality, but haven't tried them yet.

    UPDATE:
    Problem was not setting "allowDestroyingAssets" in DestroyImmediate to true, most likely a problem only in Editor
     
    Last edited: Feb 2, 2021