Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Periodic 6ms garbage collection spikes in empty scene? Is this normal?

Discussion in 'Editor & General Support' started by Sinterklaas, Sep 1, 2021.

  1. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    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.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Use the profiler to figure out what is generating garbage.
     
  3. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    The garbage collection is listed under TimeUpdate.WaitForLastPresentationAndUpdateTime in the profiler. I don't really know what that means.

    But like I said, it happens even in an empty scene, so I'm quite certain it's not caused by any scripts I've written.
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Yes but it could be a package you have installed.
     
  5. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    I haven't installed any packages, so that seems unlikely. My project is a default URP project.

    But am I correct to assume, judging from your responses, that periodic 4-8ms GC spikes are in fact not normal behaviour?
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    URP is a package.

    By default Unity installs a whole bunch of packages.

    You can see in the profiler what is generating garbage and how much
    upload_2021-9-1_20-35-54.png
     
    Joe-Censored likes this.
  7. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    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.

    Observe:
    upload_2021-9-1_19-50-48.png
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Go to player settings and untick Use incremental GC and see if this goes away.
     
  9. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    That actually totally worked, and the microstutter seems to be mostly gone too now.

    Thanks a bunch!
     
    AcidArrow likes this.
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
    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.
     
    Joe-Censored likes this.
  11. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,521
    In this case it may be useful to have the checkbox only effect the build and not the editor?
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
    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.
     
    Joe-Censored likes this.
  13. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    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.
     
  14. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    I’m confused.
     
    FlightOfOne likes this.
  15. Sinterklaas

    Sinterklaas

    Joined:
    Jun 6, 2018
    Posts:
    93
    I misremembered, lol. I thought I'd checked, but I guess I forgot.

    Anyway, I just found out that you can profile built games, and yes I can confirm that the GC spikes don't occur in built games. Mystery solved!
     
  16. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,085
    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.



    #Edit forgot to mentioned that's during playmode
     
  17. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,646
    I have no idea, I don't really work on the editor.
     
  18. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This

    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.
     
  19. Lars-Steenhoff

    Lars-Steenhoff

    Joined:
    Aug 7, 2007
    Posts:
    3,521
    Issue is so bad in the editor it is slowing your work progress.

    Yes for me entering playmode is the issue, even without domain reload it takes like 15 seconds and thats just putting me out of flow.
     
  20. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,432
    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.
     
  21. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,432
    There are plans but I'm not sure about the progress. From my perspective what I can say is that we are aiming to be entirely done converting the Memory Profiler package to UI TK for 2022.2 but are still lacking some more complex general controls that are also needed by other editor UI like the Profiler window, namely a multicolumned TreeView, so things like that can slow that conversion process down.
     
  22. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,085
    Depends what you're testing. Looking for gc allocs is fine in editor but requires some knowledge what can be ignored (for ex. GetComponent and setting tmp.text). My productivity would drop 5 times if I had to make a build every change when optimizing.

    That's not everything - many projects (including mine) require good performance in editor playmode for quick iterations.
     
    Joe-Censored likes this.
  23. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,085
    Since Profiler can be opened in separate process that's not a big issue, I rather meant anything else that renders every frame during playmode, that looks to be only game and scene views:


    At least scene view can be hidden but there's no way of getting rid of game view top bar even in "maximized" mode.
     
    Last edited: Sep 2, 2021
  24. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    Wait, what???
    I have been trying to figure this out for a very long time. I always see this GC on URP when there are multiple cameras. Makes no sense but, I shall give it a try! Thanks!
     
  25. arthaqss

    arthaqss

    Joined:
    Mar 10, 2021
    Posts:
    3
    Trying to solve this for last 2 days.
    Ticking the Use incremental GC actually helps. But I'm wondering what causes the issue? Is it really just the URP package?
    I just created an empty project, empty scene, URP and the 14-25 ms spikes are there.
    I'm going to tick the check box but if anyone knows whether this is a bug or not? It's driving me crazy not being able to find a fix o_O
    If bug - any ETA/ticket on the fix?

    Tested in 2023.2.1f1 and 2021.3.23f1.
     
  26. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,432
    If in doubt, file a bug report. I don't think one exists and if it does, a duplicate won't hurt it's resolution speed.