Allocation is not collection time

Garbage collection (GC) reclaims managed C# memory that is no longer referenced. Collection can make a frame slower. GC.Alloc measures newly allocated managed memory, not time spent collecting it. Investigate where allocations were created separately from the later frames in which collection runs.

Measure with the Profiler

  1. Record the same workload in a Development Build on the target device.
    GC.Alloc ≠ GC time
    GC.Alloc ≠ GC time
  2. Inspect the GC.Alloc column in CPU Usage Hierarchy and enable allocation call stacks when needed.
    GC.Alloc ≠ GC time
    GC.Alloc ≠ GC time
  3. Review repeated strings, arrays and collections; reuse where appropriate and measure again under the same conditions.
    GC.Alloc ≠ GC time
    GC.Alloc ≠ GC time

Editor measurements may differ from Player builds. Incremental GC distributes collection work over frames, but does not replace reducing unnecessary allocations or device profiling.

See also LINQ and GC.Alloc.

References

Unity 2022.3: Garbage collection best practices / Garbage collector overview