- Home /
Why is my Coroutine Continuing to allocate GC from the "MoveNext()" After it completes?
I try to be straightforward in my titles... it's exactly what it sounds like.
I simply call a single Coroutine, which does not call any other coroutines, at any time... I call this single coroutine via the "StartCoroutine(Coroutine())" method at the Start() method.. (this also occurs when I call it from Awake or anywhere really.)
Shouldn't it only be getting called once???
Nevertheless, I am getting MAJOR Lag Spikes from GC Alloc according to the profiler, from this coroutine being repeatedly called... this issue persists in numerous scripts I've written. I'm completely confused rn; I'm about ready to never use Coroutines again...
https://pasteboard.co/HmBHlzE.png (The Image Previewer is not working... lol)
Also, I am NOT calling it from ANYWHERE else, even though it says it's getting called from the Update method, I have triple and quadruple checked with our 'ol friend Ctrl+F... it's ONLY in Start().
Any input is appreciated.
UPDATE:
So I've FINALLY found out what is causing it to continue calling the Coroutine.. though it doesn't make sense.. here's my test:
Code (CSharp):
private IEnumerator Test()
{
while (brainStarted != BrainStarted.True)
{
debugLog = "WAITNG....";
yield return null;
}
debugLog += " - EXITED";
}
and of course it DOES meet the exit condition after a short time... and surely enough, I DO get the " - EXITED" logged. (debuglog is just a reference to a static UI text Component) although the conditions are met, it continues to call the Coroutine.... I'm continuing investigation - still input is welcome from anyone. The issue is pertaining to while loops inside of Coroutines... this is very strange.
UPDATE:
So only a minor update, but I also noticed this:
https://pasteboard.co/HmBViNr.png
And again, this coroutine (Test()) is merely called one time in the Monobehaviour.Start() method. Via StartCoroutine(Test());
I changed the exit condition from my enum to a bool to verify the issue is not related to the enum... the issue persists.
UPDATE:
Here's an isolated Class I've built for proper testing (still occurring):
Code (CSharp):
using BrainConnect;
using System.Collections;
using UnityEngine;
public class Test : MonoBehaviour {
private bool brainReady = false;
void Start ()
{
ConnectBrain.BrainReady += BrainReadyEvent;
StartCoroutine(TestEnumerator());
}
private void BrainReadyEvent(object s, System.EventArgs e)
{
brainReady = true;
}
private IEnumerator TestEnumerator()
{
while (!brainReady)
{
ConnectBrain.debugLog = "WATING....";
yield return null;
}
ConnectBrain.debugLog += " - EXITED";
}
}
This new isolated script is simply attached to a new Empty GameObject Parented under nothing. I get the same results.
I'm having a similar issue with 10$$anonymous$$B of GC allocated a some specific times on Coroutine.$$anonymous$$oveNext
[Edit] : Do you have any update on this ?
No, just no. You can't have the exact same issue as you do not have the exact same code. We don't even know all of his code so it's impossible to know.
If you have a question, please Ask a Question and do not post an answer to another question that doesn't answer the question at all. Also if you want any progress with your specific issue you have to include much more details in your question. Show the exact code you're using and as i said in my answer here, include a deep profile.
@Bunny83 Chill down please. First I was not talking to you. 2nd I'm having exactly the same issue and symptoms as @DDeathlonger with a similar code, beeing : "How the hell do you debug those GC.Alloc from Coroutine.$$anonymous$$oveNext()" and I wanted to know if he had any follow up.
The only thing I can agree of is I should have written a comment though I'm always trying to juggle with the absolutely not straightforward 4 support channels forums+answers+qa+bug reports of unity.
I should have taken more time to answer this, but you're no one to judge. Finally the interesting part of your answer is "include a deep profile". Which is the only thing I was asking for. Hope it'll help though.
Answer by Bunny83 · Jul 24, 2018 at 01:55 PM
Sorry but your profiler results clearly do not belong to the code you've shown. In your image of your first update you clearly use something like ReadPixels inside that offending coroutine and this call is causing the memory allocation, not the coroutine itself.
Next is we don't really see which frame of the profiler we're currently looking at.
The next suspicious thing is that the profiler shows the MoveNext call inside the Behaviour Update of your script. Coroutines are usually continued from the "Update.ScriptRunDelayedDynamicFrameRate". There you'll find the "CoroutinesDelayedCalls" where the actual coroutine would be continued.
To me that looks like you somehow manually try to call MoveNext on an instance of your IEnumerator. However your code clearly doesn't match your results.
Your last image also shows the MoveNext call from inside of the Update call of your ConnectBrain class.
You haven't mentioned which Unity version you're using. The exact point from where the coroutine may be continued automatically may have changed. My observations has been done on Unity 2018.1.3f1 (I just saw that you used 2017.3.1f1 on your screen shot)
Anyways your code doesn't look like it's matching your profiler results. You may want to turn on deep profile at the top of the profiler to basically get a complete stack trace inside the profiler.
If you need still help with this you have to include more details, a deep profile picture and maybe the exact code you're using inside a new empty project without any other code interfering.
This helped to get the source of the issue :
You may want to turn on deep profile at the top of the profiler to basically get a complete stack trace inside the profiler.
Your answer
