The garbage collection mode can be configured in the following ways:
GarbageCollector class to manually control the garbage collector, or disable it completely.System.GC.Collect to perform a full, blocking garbage collection.
To disable incremental garbage collection:
Note: Incremental garbage collection isn’t available for the CoreCLR scripting back end.
Incremental garbage collection is useful in most Unity projects, especially if the project has garbage collection spikes. However, incremental garbage collection adds write barriers to any calls that change references, so you might want to disable it if your project doesn’t trigger garbage collection in performance critical sections. Disabling incremental garbage collection in this situation can improve the performance of your scripting code in your project.
Use the Profiler to verify that your application performs as you expect. If you analyze a Profiler capture in isolation, it can be difficult to find out how incremental garbage collection affects performance. It’s best practice to profile the same performance critical section twice: once with incremental garbage collection enabled, and once with incremental garbage collection disabled. You can then compare both Profiler captures with the Profile Analyzer package. For CPU bound projects, the difference can be as much as 1 ms per frame.
For more information on disabling garbage collection, refer to Garbage collection modes.
You can use the APIs in the GarbageCollector class to manually control or disable the garbage collector for the Mono and IL2CPP scripting back ends. You can use the following APIs to control the garbage collector:
GarbageCollector.Mode.Disabled: Setting GarbageCollector.GCMode to this fully disables the garbage collector. Using System.GC.Collect in this mode has no effect.GarbageCollector.Mode.Manual: Setting GarbageCollector.GCMode to this fully disables automatic invocations of the garbage collector, but you can still use System.GC.Collect to run a full collection.GarbageCollector.CollectIncremental: Runs the garbage collector incrementally.
Note: The GarbageCollector API isn’t supported for the CoreCLR scritping back end.
To disable the garbage collector completely, set GarbageCollector.GCMode to Disabled. When you disable the garbage collector, Unity doesn’t perform any garbage collection. Calling System.GC.Collect has no effect and doesn’t start a collection.
Disabling the garbage collector prevents CPU spikes, but the memory usage of your application never decreases, because the garbage collector doesn’t collect objects that no longer have any references.
Disabling the garbage collector requires careful memory management. If you don’t manage memory carefully, the managed heap continuously expands until your application runs out of memory, and the operating system shuts it down.
Ideally, you should allocate all memory before you disable the garbage collector and avoid additional allocations while it is disabled.
Manual collection gives you control over when collections happen, so you can fine-tune the smoothness of your application’s content or memory usage. Manual collections can be invoked regardless of whether GarbageCollector.GCMode is Manual or not.
You can trigger a manual garbage collection in one of the following ways:
System.GC.Collect.GarbageCollector.CollectIncremental.