T
T
Timofey Bulgarian2019-01-30 23:17:50
Unity
Timofey Bulgarian, 2019-01-30 23:17:50

How to solve memory leak in Unity3d for textures?

In Unity, it was necessary to convert the image from the camera to png and then send it over the socket. For this, RenderTexture was used and here is a piece of code.

byte[] RenderTextureToPng(RenderTexture rt)
    {
        Texture2D png_tex = new Texture2D(512, 512, TextureFormat.RGB24, false);
        RenderTexture.active = rt;
        png_tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
        png_tex.Apply();
        return png_tex.EncodeToPNG();
    }

But it suddenly turned out that after starting the program, the space occupied by Texture in the Memory tab began to increase sharply, loading all possible memory (even the system fell from this the first time I didn’t notice).
What could it be? When the code for calling this function is commented, everything is fine.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timofey Bolgarin, 2019-01-30
@Arhin

Solution:
https://forum.kerbalspaceprogram.com/index.php?/to...
It was a constant announcement
Although I still do not fully understand why it is not removed by the garbage collector ..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question