Why do I get a memory leak when loading a png and set to material?

Hello,

my Code is giving me Memory leaks, each time I reload an Image 5-10 mb are gone.

I tried to set material.maintexture or material.SetTexture, it happens in both cases.

My Code to set the texture:

    public void ShowImage()
    {
        {
            try
            {
                ImagePlane.SetActive(true);
                if (Globals.ExpertData.TexturePaths != null && !string.IsNullOrEmpty(Globals.ExpertData.TexturePaths[Globals.Step]))
                {
                    ImagePlane.GetComponent<MeshRenderer>().material.SetTexture("_MainTex", Globals.LoadImage(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Globals.ExpertData.TexturePaths[Globals.Step]));
                    //ImagePlane.GetComponent<MeshRenderer>().material.mainTexture = Globals.LoadImage(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Globals.ExpertData.TexturePaths[Globals.Step]);
        }
    }

Loading the Image from file:

    public static Texture2D LoadImage(string fileName)
    {
        Debug.Log("Loading Image: " + fileName);
        Texture2D tex = null;
        byte[] fileData;
        try
        {
            if (File.Exists(fileName))
            {
                fileData = File.ReadAllBytes(fileName);
                tex = new Texture2D(2, 2);
                tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.

                return tex;
            }
            else
            {
                Debug.LogError("Image cannot be found: " + fileName);
                return null;
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("Error loading image: " + ex.ToString());
            return null;
        }
    }

Can anybody see, why I am losing Memory here? I opened the profiler and “Memory” there, each time I call this method Textures + 1 and + 5-10 mb…

thanks!

Sorry I posted in the wrong Forum and cannot move it anywhere else, please move it…

By the way, the file loading is not the Problem.

When I use
tex = new Texture2D(2000, 2000);
and do not load at load the Memory leak gets even worse…

Thanks

I thought it might be the static LoadImage, but it is not. When I do this, I still have my Memory leak:

    public void ShowImage()
    {
        {
            try
            {
                ImagePlane.SetActive(true);
                if (Globals.ExpertData.TexturePaths != null && !string.IsNullOrEmpty(Globals.ExpertData.TexturePaths[Globals.Step]))
                {
                    //ImagePlane.GetComponent<MeshRenderer>().material.mainTexture = Globals.LoadImage(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Globals.ExpertData.TexturePaths[Globals.Step]);
                    Texture2D tex = new Texture2D(2, 2);
                    tex.LoadImage(File.ReadAllBytes(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Globals.ExpertData.TexturePaths[Globals.Step]));
                    ImagePlane.GetComponent<MeshRenderer>().material.mainTexture = tex;

maybe could try Destroy() the old texture? or can you just load into the existing texture, without creating new?

1 Like

It seems the local temporary texture is not being release when the void Ends.
I made a global texture now which I use everytime, no the Memory leak is gone.

Thanks

1 Like

That is correct. You need to destroy textures (and other assets) that you create at runtime. Generally the pattern is when you’re done with it, destroy it. If you still have one “from before” and you want to create a new one, destroy the old one, then create.

However, textures that you “load” from Resources.Load, from Asset Bundles, or from Texture reference fields do not need to be destroyed, and furthermore CANNOT be destroyed. You are simply getting a pointer to something that cannot be changed at runtime in those latter cases.

1 Like

Thanks for the info guys!

1 Like