Creating a gallery made of Screenshots in persistentData creates memory leak

Hi,

I am trying to create a Custom Gallery inside Unity made of screenshots in runtime, but each time that I take a screenshot , memory increase like 30 mb on IOS Devices even if a compress and encode the texture to JPG .

Here is part of the code to save Texture (I dont see problem with this) :

    private void SaveScreenShotInPath() //Called from Action Button
    {
       
        Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        ss.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
        ss.Apply();

        string filePath = Path.Combine(Application.persistentDataPath, "HyperLoop_Hardt_" + DateNowString() + ".jpg");
        File.WriteAllBytes(filePath, ss.EncodeToJPG(qualityImage));     

        StartCoroutine(AddTexture(filePath));
       
        Destroy(ss);
        Resources.UnloadUnusedAssets();
       
    }

Here is the code to get texture from persistent Data (When I add the texture to a gameobject is when increase the memory 30 mbs even is texture is compressed):

    public IEnumerator AddTexture(string path)
    {
        UnityWebRequest www = UnityWebRequestTexture.GetTexture("file://" + path);
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError)
        {
            Debug.Log(www.error);
        }
        else
        {
            var texture = ((DownloadHandlerTexture)www.downloadHandler).texture;
        
            texture.EncodeToJPG(qualityImage);
            texture.Compress(false);
            carrousel.AddImageToCarrousel(texture);

            SetPreviewImage(texture);

            yield return null;
        }

        yield return null;

    }

After adding 50 screenshots , app crashes because of RAM memory.

Maybe is a crazy idea make a Gallery of Screenshots inside of Unity ?? :smile: