Gc.Collect locations on the Profiler

When using the Unity3d profiler, I can’t help but notice the recurring GC spikes. This topic has been exhaustively discussed on unityAnswers, of that I am aware.

What I have my doubts about is the relevance of WHERE the GC.collect() appears. In other words, if the GC.collect() method appears as a child of another function, does that mean that such parent function is generating a considerable amount of garbage? Or does it mean that the GC’s heap got full and GC.collect() just so happened to be called inside a given parent function?

Since I am here, another fact that I am curious about : is it possible to occur more than one call of GC.collect() in the same frame, maybe even as parallel processes?

Any help is welcome, thanks in advance!

In the profiler, be sure your view is set to ‘GroupHierachy’. This can be found right above the list of functions and the time it takes to complete each one.

If there was a garbage collection on the frame you are viewing, you should see a GarbageCollection section under the list of functions. Expand it to see any collections that happened during that frame. I’ve never seen Garbage collection be listed under another function. (Note: I can never find GC.Collect() or GarbageCollection in the list if I’m set to ‘Hierarchy’ view. Perhaps others have?)

For a vast majority of your frames, you shouldn’t be able to find a GarbageCollection. An easy way to test this is to call GC.Collect() on one of your script’s Awake() function. If you run the profiler and view the first frame, you should see a GarbageCollection section. Note that it does not show up under your script’s Awake() function. If you go to a different frame, though, the garbage collection details will not show up (unless you had a garbage collection take place that frame).

As for if more than one collection can take place in the same frame, that I am unsure of. I would guess that only one collection would call in a single frame, but I cannot answer that with certainty.

Also, on the off chance that you are developing for iOS, I would strongly recommend the internal profiler. You can review the log after playing your game to see when the garbage collector called and how long it took. I have found it much more convenient than the editor profiler. iOS Internal Profiler