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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to free up memory in the editor?

Discussion in 'Editor & General Support' started by inkipinki, Feb 16, 2023.

  1. inkipinki


    Jun 19, 2013

    I'm working on projects where I have to clean up huge amounts of assets and usually I ran out of memory on actions like the one below. I'm using a 32gb machine and nothing else is running. Unity usually crashes around 24-28gb memory usage.

    //Convert all selected materials to "Standard" shader.
    //In this scenario, unity did not assign correct shaders to the imported materials and I had around 12k materials.... (don't ask).

    Code (CSharp):
    1.         List<Material> materials = new List<Material>();
    3.         foreach (var item in Selection.objects)
    4.         {
    5.             if (item is Material)
    6.             {
    7.                 Debug.Log(item.ToString());
    8.                 materials.Add(item as Material);
    9.             }
    10.         }
    12.         Selection.objects = null;
    14.         Shader shader = Shader.Find("Standard");
    16.         foreach (var item in materials)
    17.         {
    18.             item.shader = shader;
    20.             Resources.UnloadUnusedAssets();
    21.             System.GC.Collect();
    22.         }
    On an other project I had to convert textures in the project (and wanted to use unity). There I've used Object.Destroy to destroy the texture that has been read with GetPixels(), but it was throwing errors that I should not use Destroy. (although it fixed the memory leak issue).

    On a third note.. I've read that RenderTexture.Release() does not immediately release the memory but only after a few frames of that asset not being used. Does that mean that RenderTexture.Release is unusable in editor because you don't generate frames? (I've tried to use it to read "not readable" textures.

    Unity themselves also suffer from these kinda memory issues, like the one below got thrown when I've used the built-in material upgrader (standard to hdrp):

    /s So as it seems this post could not just help me but the guys over there. :)

    Could someone point out what am I messing up?

    Last edited: Feb 16, 2023
  2. Kurt-Dekker


    Mar 16, 2013
    Yuck. I hate limitations like this.

    The oldskool way I've done these sorts of bulk changes that choke the editor is to go outside of Unity and search / replace things in the asset YAML files.

    For instance if you change the Shader on a given Material, you can look in the Material file and see how it changed.

    From that you can craft a search / replace that operates on all .mat files and does the same thing, then let Unity reimport them all.

    I've even gone so far as to write custom python scripts to bulk-manipulate assets, such as the gizmo highlight icons which until recently could only be set one at a time in the editor.
  3. spiney199


    Feb 11, 2021
    In the editor you should use Object.DestroyImmediate() to clear objects lying around in memory.