Simple spawn script memory leak

I’m working on a game and I noticed a memory leak.
I managed to reduce the code causing it to this small piece of code:

public class SpawnTest : MonoBehaviour
{
    public GameObject obj;

   private IEnumerator Start ()
    {
        while (true)
        {
            var instance = Instantiate(obj);
            instance.transform.parent = obj.transform.parent;
            var starttime = Time.time;
            float duration = 0.5f;
            var text = instance.GetComponent<Text>();
            while (Time.time < starttime + duration)
            {
                var progress = (Time.time - starttime) / duration;
                var color = text.color;
                color.a = progress;
                text.color = color;
                yield return null;
            }
            Destroy(instance);
        }
   }
}

So what I have in the scene is just a gameobject with only this script attached, and a UI.Text object (which is set as the obj in this script).

This causes the memory to increase by about 10 MB/second, but I have no idea why.
It seems like a bug in Unity

Can anyone tell me how to fix this?

Btw, when I replace the line “color.a = progress;” with “color.a = 1;” there is no memory leak,
why?

(also, why can’t I add usefull tags? it only suggests useless ones, and I can’t create new ones)

“yield return null” without being called from “StartCoroutine()” is that a bug in your script, I don’t know if that’s defined behaviour?

That’s not a bug, the Start function can be a coroutine.
I’d link you the documentation, but I can’t seem to find it (although I’m sure I’ve seen it before)

Also “yield return null” makes it yield until the next frame

It’s here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

I read it before posting, quote: “When using JavaScript it is not necessary to use StartCoroutine, the compiler will do this for you. When writing C# code you must call StartCoroutine.”

I think that doesn’t apply for Start and other Unity methods.

That page doesn’t actually say anything about the start function being a coroutine, but they do use it as one in one of the code examples.

Regardless, the start function can be used as a coroutine, that’s a fact,
But I don’t even use it in the actual game code, so it’s irrelevant to the actual problem at hand, namely the memory leak.

I’m trying to be nice.

I’m pretty sure that (by omission) you don’t know or care what a “memory leak” is, and that you have seen some metric of memory use go up in some tool like Windows Task Manager…

I’m telling you where I see the issue is, I think the least you could do is humour me enough to try the one line change to confirm if it has any impact on your memory use issue.

Tried this out of curiousity, and didn’t encounter any leak or growing memory…

And no one is being overly mean…

Youre telling them where you THINK the issue is. Start being an IEnumerator and using yield return null there is perfectly valid and has no reason to cause a leak.

I havent tried it myself but I can assume this portion of code is not the issue. :wink:

A recent Unity version actually has a memory leak so it could be that. Are you on Unity 2017? (Donjt know the specific version)

Not really sure how the UI Text element works but I’m suspicious that changing its color might be the cause of the ‘leak’. It might not even but a leak but just a lot of garbage - although 10 MB/sec is a lot of garbage!

Generally speaking, uGUI is well known for its performance issues when dealing with constantly changing UI elements. At the very least, that color change is likely causing the UI to rebuild it’s mesh. It might even be creating a new material every frame! I’d suggest you try either a deep-profile, profiling in an actual build, or even just removing the change to the UI element and see what that does. In a pinch you could go download the uGUI source code and have a look to see if changing the color of the text has any large scale impacts.

Also, this UI element you are parenting to the rest of the gameobject. Are there many other UI elements on that same gameObject? If there are and they don’t change then that could be another issue as such a change to the canvas requires the entire canvas UI mesh to be completely rebuilt.

I’m sorry, I didn’t mean to be rude. I was just annoyed by the memory leak.

Maybe I should have mentioned that I used the profiler to look for the leak, that’s how I found it was the ui.text element that caused the mesh memory to go up.

And I actually did try what you suggested, as I don’t even use the start function as a coroutine in the actual game code, so I’m sure it’s not what causes the memory leak.

Yes, I am using the 2017 version, namely 2017.1.0f3
Do you happen to know if Unity fixed it in a more recent version?
I’ll update my Unity then

Yes, it is the color change that seems to cause it, as when I replace the line “color.a = progress;” with “color.a = 1;” there is no memory leak.
Also, I should mention that I added a couple of outline and shadow components to the text element, without it the memory leak is a lot smaller.
I did profile an actual build and it behaves the same.

In the actual game, I’m using this to display points the player gains
Maybe I should just not use the UI system for this then?
But then it will be more complicated to display numbers without the ui system. :s

What Unity version where you using?

2017.2.0f3

Ah, great, perhaps they did fix it in the newer version.
I’ll update my Unity and will let you know how it goes

I updated my Unity to the newest version and the memory leak seems to be fixed.
So it really was a bug in Unity

Thanks for the help everyone!

Glad to hear you got it worked out :slight_smile: