- Home /
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!
Answer by Keryu · Dec 08, 2011 at 07:49 PM
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
Thanks for the response $$anonymous$$eryu!
$$anonymous$$y question was mainly inspired because I often see CG.Collect
calls in the "Hierarchy" view, but for some reason it appears under different parent functions during each call. $$anonymous$$aybe I didn't make myself clear, but I never witnessed a CG.Collect
call at the root of the "Hierarchy" view, differently from the "GroupHierarchy" view. What made me curious was the "why" of the varying parent functions in the "Hierarchy" view in the Profiler. Are these parent functions generating most of the garbage (which would be good, because it would serve as a tip to where to start my optimizations), or did CG.Collect
just happen to be called while the given parent function was executing?
Your answer
![](https://koobas.hobune.stream/wayback/20220613061916im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is GC.Collect() very slow? 2 Answers
What causes an increase in editor GUI garbage collection? 2 Answers
Mono profiler hooking on Unity 2 Answers