Why is this Texture2D memory leak occurring?

I have a script which is loading a photo into a texture (photoThumbT2D), then rotating it:

                byte[] byteArray = File.ReadAllBytes(path);
           
                UnityEngine.Object.Destroy(photoThumbT2D);
           
                photoThumbT2D = new Texture2D(0, 0);
                photoThumbT2D.LoadImage(byteArray);

                photoThumbT2D = RotateImage(photoThumbT2D, -90);

Where the RotateImage function is adapted from here as:

public static Texture2D RotateImage(Texture2D originTexture, float angle) {
        int width = originTexture.width;
        int height = originTexture.height;
        float halfHeight = height * 0.5f;
        float halfWidth = width * 0.5f;

        Color32[] originPixels = originTexture.GetPixels32();
        Color32[] rotatedPixels = originTexture.GetPixels32();

        int oldX;
        int oldY;

        float phi = Mathf.Deg2Rad * angle;
        float cosPhi = Mathf.Cos(phi);
        float sinPhi = Mathf.Sin(phi);
   
        for (int newY = 0; newY < height; newY++) {
            for (int newX = 0; newX < width; newX++) {
                rotatedPixels[newY * width + newX] = new Color32(0, 0, 0, 0);
                int newXNormToCenter = newX - width / 2;
                int newYNormToCenter = newY - height / 2;
                oldX = (int)(cosPhi * newXNormToCenter + sinPhi * newYNormToCenter + halfWidth);
                oldY = (int)(-sinPhi * newXNormToCenter + cosPhi * newYNormToCenter + halfHeight);
                bool InsideImageBounds = (oldX > -1) && (oldX < width) && (oldY > -1) && (oldY < height);

                if (InsideImageBounds) {
                    rotatedPixels[newY * width + newX] = originPixels[oldY * width + oldX];
                }
            }
        }

        Object.DestroyImmediate(originTexture);
   
        Texture2D result = new Texture2D(width, height);
        result.SetPixels32(rotatedPixels);
        result.Apply();
        return result;
    }

However, this application of RotateImage is leading to a memory leak where the more times it runs the more memory usage piles up in the profiler.

Is there a correct way to clear the textures being created by this? Thanks.

Update: This was a bug, posted about it here: Memory management is broken in Android if >500 MB memory usage (ie. any UI Toolkit build)

Can you tell what is piling up? Is it the Texture2D? Are you sure nothing that is calling this is hanging onto the destroyed original texture?

1 Like

DestroyImmediate() isn’t supposed to be used in play mode. I would suggest replacing it with Destroy(). See in the unity docs. Unity - Scripting API: Object.DestroyImmediate