I am posting this message for someone to ease my worried mind about the “new” operator used in the very vital “yield return new WaitForSeconds(…)” instruction.
As I searched the documentation and the forums, I didn’t get any clarification wether or not this instruction is very memory consuming since it is used a lot in my code and every time it creates (or not) extra objects in the heap.
To summarize, I am actually asking if it is ok to use it like this in the future or if there is a better, more memory efficient solution. Maybe I’m just paranoid but good programming practices suggest that the “new” operator must be used as seldom as possible.
YieldInstruction is a class, rather than a value type, so using new WaitForSeconds() does perform a memory allocation on the heap each time, though it’s a fairly small one.
In game development (for managed languages at least), the biggest problem with frequent memory allocations is not the amount of memory allocated (usually), but rather the performance hit of the periodic garbage collection that occurs.
By ‘frequent’ I mean on every frame. If you are allocating memory of any kind on every frame, you are probably going to have to rethink that at some point.
Having said that, I wish people would run across the following piece of advice as frequently as they run across all of the “don’t do this”, “don’t do that”, bits of ‘conventional wisdom’ : Measure, measure, measure!!! See if it’s really a problem for you by profiling, measuring, and testing. Why fix a problem that’s not really a problem in actual practice?