Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Memory issues creating lots of screenshots

Discussion in 'Editor & General Support' started by NiklasHarfmann, Jan 26, 2022.

  1. NiklasHarfmann

    NiklasHarfmann

    Joined:
    Jul 5, 2021
    Posts:
    2
    Code (CSharp):
    1. private static void CreateImage(string location, Camera cam, Texture2D mask = null)
    2.         {
    3.             var temporaryCam = Instantiate(cam);
    4.  
    5.             var temporaryTexture = RenderTexture.GetTemporary(2560, 1440, 24);
    6.  
    7.             temporaryCam.targetTexture = temporaryTexture;
    8.             RenderTexture.active = temporaryTexture;
    9.  
    10.              temporaryCam.Render();
    11.  
    12.             texture2D.ReadPixels(new Rect(0, 0, 2560, 1440), 0, 0);
    13.  
    14.             //applying the mask if it exists
    15.             if (mask)
    16.             {
    17.                 for (int j = 0; j < mask.height; j++)
    18.                 {
    19.                     for (int i = 0; i < mask.width; i++)
    20.                     {
    21.                         if (Math.Abs(mask.GetPixel(i, j).r - 1) < 0.1 && Math.Abs(mask.GetPixel(i, j).g - 0) < 0.1 &&
    22.                             Math.Abs(mask.GetPixel(i, j).b - 0) < 0.1)
    23.                         {
    24.                             texture2D.SetPixel(i, j, new Color(0, 0, 0, 0));
    25.                         }
    26.                     }
    27.                 }
    28.              
    29.                 DestroyImmediate(mask);
    30.                 texture2D.Apply(false);
    31.             }
    32.  
    33.             var bytes = texture2D.EncodeToPNG();
    34.  
    35.             //saving the file in location
    36.             WriteAllBytes(location, bytes);
    37.  
    38.  
    39.            bytes = null;
    40.            temporaryCam.targetTexture = null;
    41.            Destroy(temporaryCam.gameObject);
    42.  
    43.            temporaryTexture.Release();
    44.            Destroy(temporaryTexture);
    Hello! I am currently running into a memory leak creating lots of screenshots. Basically the GPU memory fills up pretty fast. Considering the size of the picture that's expected. But shouldn't the memory be released? My texture2D is a global one so i dont have to create and delete a bunch of them. Appreciate every bit of help or any other suggestions :thinking:
     
  2. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,105
    I'm a newbie on this topic but maybe try separate the whole function from any other object that exist outside the function. like that global texture2D.

    maybe because the texture2D object is not destroyed the code will not prioritize the destruction of the textures created in the function.

    so have the function take some inputs, do its stuff and then go away, all safe contained.

    maybe it will work faster.

    also search google for "garbage collection in unity" there is a bunch of info that may help