Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Task.Run() garbage frame time spikes

Discussion in 'Scripting' started by illinar, Jan 4, 2020.

  1. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    857
    I'm running an async method and it apparantly allocates a lot of garbage and creates massive frame time spikes. The spikes show up as Other and EditorLoop and not as GC but still I assume that's GC.

    Code (CSharp):
    1. protected override JobHandle OnUpdate(JobHandle handle)
    2.     {
    3.         Entities.ForEach((MapMagicReference mmr) => { _mm = mmr.Value; }).WithoutBurst().Run();
    4.  
    5.         if (!_generating)
    6.         {
    7.             GenerateMap(_mm);
    8.         }
    9.         return handle;
    10.     }
    11.  
    12.     async private Task GenerateMap(MapMagic.MapMagic mm)
    13.     {
    14.         await Task.Run(() =>
    15.         {
    16.             _generating = true;
    17.             var coordRect = new CoordRect(new Coord(0, 0), new Coord(128, 128));
    18.             var results = new Chunk.Results();
    19.             var borderSize = 16;
    20.             var generateSize = 64;
    21.             var size = new Chunk.Size(generateSize, generateSize, generateSize);
    22.             var seed = 0;
    23.             mm.gens.Calculate(coordRect, results, size, seed);
    24.             _generating = false;
    25.         });
    26.     }
    27.  

    upload_2020-1-4_15-12-28.png

    What can I do to avoid the garbage but still run my method Async().
     

    Attached Files:

  2. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    857
    This might have nothing to do with that async task lamda. This might just be the method call inside it that generates the garbage. I'm investigating.
     
  3. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,168
    I know those spikes ;) that's the Profiler Window repainting, which isn't happening every frame but only every couple of frames to reduce the general EditorLoop overhead when profiling the Editor or Playmode. BTW, if you ever want to see what is happening inside of the EditorLoop, switch the target from Playmode to Editor. To see you games performance without this overhead, make a development build and profile that.
     
  4. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,168
    That said, it does seem to coincide with GC collection. Maybe repainting the Profiler puts the Memory pressure on to cause a GC within the Editor code, which is why you might not see it when targeting Playmode. So yes, try profiling the Editor while playing in Playmode. You might want to turn on call stacks collection and let the Profiler window show the memory usage details instead of the CPU usage views while profiling.
     
  5. odoluca

    odoluca

    Joined:
    Nov 5, 2014
    Posts:
    28
    Have you resolved this?
     
  6. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    857
    So that was the editor? Profiler repaint. That makes sense. Thanks, @MartinTilo

    Close (or stop) the profiler and it shouldn't be a problem? But really, as @MartinTilo said, profile the editor to see if that's the editor.
     
    MartinTilo likes this.