'C# IEnumerator memory concerns and garbage collection

I am working in Unity on a so called "Scriptable Event Sequencer" where a system executes a collection of IEnumerators in a set order. This is a game, so quite often the coroutines need to be halted and wait for specific conditions. In Unity this is most easily done through WaitUntil.

A lambda expression is used to define the conditions for each WaitUntil call. This is where my concerns begin. Each of these coroutines must be started, which allocates memory. Furthermore each lambda expression I use within these coroutines to be best of my knowledge (correct me if I'm wrong) translates to a Closure because usually non-local variables are involved, which in turn is also relatively memory-intensive for as far as I understand.

//Instance of coroutine runs only once, does not repeat, but checks each frame for "loadFinish"
public override IEnumerator MyRoutine()
{
    yield return new WaitUntil(() => stateData.loadFinish == true);
    stateData.loadFinish = false;
    
    // do stuff
    
    yield return null;
}

In the example above, are there any insights or opinions that come to mind of the memory-efficiency of this code?

I must admit, I have not done any concrete profiling yet and am not the most knowledgable on the subject and most of my knowledge comes from this article. As far as I know, C# has automatic garbage collection and I would guess that things like what I described would count as garbage. At the same time however I have also read stories of actually running out of memory and crashing due to oversights like this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source