By default, Unity carries out garbage collection incrementally. If you want more control over when and how garbage collection happens, you have the following options:
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:
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 ProfilerA window that helps you to optimize your game. It shows how much time is spent in the various areas of your game. For example, it can report the percentage of time spent rendering, animating, or in your game logic. More info
See in Glossary 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 the garbage collector, or disable it completely. 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.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.
To disable automatic garbage collection and manually choose when to run it, set GarbageCollector.GCMode
to Manual
.
This disables automatic invocations of the garbage collector, but still allows you to manually perform garbage collection. Manual collection gives you control over when collections happen, so you can fine-tune the smoothness of your content or your memory usage. Call either System.GC.Collect
for a full, blocking collection, or GarbageCollector.CollectIncremental
to perform incremental garbage collection.