I’ve been trying to sort out a periodic microstutter in my game that’s been driving me nuts for a while, which occurs both in the editor and in builds.
As I was learning to use the profiler, I noticed that I get periodic spikes of 4 to 8 ms from garbage collection, which I figured might be the culprit. These spikes persist even in a scene with nothing in it.
Is this normal? I am on Unity 2020.3.15f2 LTS, in case that’s relevant.
Ok, but those are all script updates though. My test scene is fully empty, and the only thing that the profiler reports to be spending time on GC is an entry in the PlayerLoop named TimeUpdate.WaitForLastPresentationAndUpdate.
The weirdest part is that, since the scene is empty, the profiler lists no garbage as being generated at all in the player loop, but somehow Unity is still periodically wasting time on cleaning it up.
Does this also happen in a built game? This is probably expected in the editor where all sorts of editor components (including the profiler) are generating garbage, but the GC shouldn’t get triggered without allocations in the player.
If you do allocate memory, in that you’d see vastly different behaviour in the player vs the editor. It’d be hard to decide which bucket your game falls into.
I’d instead suggest profiling only the builds instead, as profiling the editor in any non-trivial case will get affected by editor itself.
Actually, it never occured to me to check a build, so I just ran a couple of builds with and without the checkbox enabled, and yeah the microstutters seem to only happen in the editor. So it’s probably an editor component that was generating the garbage. I don’t think I can get any GC statistics for a built game (or at least I don’t know how), but it sure seems that way.
Which is fine, a little bit of stuttering in the editor is no big deal.
Speaking of gc, @Tautvydas-Zilys are there any plans to finally replace old imgui? Drawing top of game bar takes 1ms, zoom slider alone (that 95% of users never use) 0.2ms not to mention gc allocs.
You should get in the habit of only testing performance in builds, and only investigating performance issues found in builds, unless the issue is so bad in the editor it is slowing your work progress. Using the Editor’s Play mode, you’re sharing the same main thread between your game, the Editor, the frequent updates of all the Editor windows, etc. Also your code when run in the Editor is optimized for debugging, not optimized for performance like in a build. So performance issues within the Editor don’t really mean anything as far as how players will experience your game.
Btw, you can see the editor allocations when you switch the target from Play Mode to Editor. Additionally, some Editor Only allocations are filtered out by default (like null component checks). That can be changed via the three dot menu in the hierarchy views toolbar.
Also, Hierarchy only ever shows the allocations made on the thread it currently shows, defaulting to the main thread. Other threads can have allocations in them too though. So you can switch through them in hierarchy or go to timeline view to look for magenta colored GC Alloc samples.
Also the memory profiler module shows a summary of all allocations in the frame across all threads.