Il2cpp, Manual Gc Execution

When we switched from mono to IL2CPP on Windows 64bit Standalone on 2018.3.5f1, we’ve noticed some FPS increase, but in some scenarios (complex game scenes) automatic GC is called each 9 frames and takes over 50ms each time. This is very annoying in game 60+ FPS game.

We’ve managed to disable automatic GC by
UnityEngine.Scripting.GarbageCollector.GCMode
and we can trigger System.GC.Collect when we want (default behaviour is probably optimized for mobiles).

We want to run GC once in a few minutes or when used memory reaches some threshold (like 1GB), as we could afford higher memory consumption rather then frequent FPS drops on PCs.

However doing so leads to “Too Many Heap Sections” error. So I want to ask:

  1. What EXACTLY are Heap Sections (I cannot settle with answer like you can only allocate 3.5GB memory)
  2. How can I monitor the number of Heap Sections I have currently used
  3. What is the maximum amount of Heap Sections, and how can I change it (at compile time)

Thanks!

Hello,

  1. Heap sections are large “chunks” of memory the GC allocates from the OS.
  2. I don’t think this is exposed
  3. It’s compiled into the IL2CPP runtime itself and not adjustable.

Actually, looking at the source code this looks like a bug we fixed but didn’t get backported to 2018.3 for IL2CPP. I’ll look at fixing this.

Automatic GC calls every 9 frames means you allocating a lot of memory every frame. This is not a good practice. You should try to eleminate this behaviour.