Configuring garbage collection
Set up the garbage collector in your project.
Read time 3 minutesLast updated 11 days ago
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:
- Disable incremental garbage collection
- Use the APIs in the class to manually control the garbage collector, or disable it completely.
GarbageCollector - Use to perform a full, blocking garbage collection.
System.GC.Collect
Disabling incremental garbage collection
To disable incremental garbage collection:
- Open the Project Settings window (Edit > )Project Settings
?
- Navigate to the Configuration settings (Player > Other Settings > Configuration)
- Disable the Use Incremental GC checkbox.
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.
Using the GarbageCollector API
You can use the APIs in the class to manually control the garbage collector, or disable it completely. You can use the following APIs to control the garbage collector:
GarbageCollector- : Setting GarbageCollector.GCMode to this fully disables the garbage collector. Using
GarbageCollector.Mode.Disabledin this mode has no effect.System.GC.Collect - : Setting GarbageCollector.GCMode to this fully disables automatic invocations of the garbage collector, but you can still use
GarbageCollector.Mode.Manualto run a full collection.System.GC.Collect - : Runs the garbage collector incrementally.
GarbageCollector.CollectIncremental
Disabling the garbage collector
To disable the garbage collector completely, set to . When you disable the garbage collector, Unity doesn't perform any garbage collection. Calling has no effect and doesn't start a collection.
GarbageCollector.GCModeDisabledSystem.GC.CollectDisabling 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.
Manually run the garbage collector
To disable automatic garbage collection and manually choose when to run it, set to .
GarbageCollector.GCModeManualThis 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 for a full, blocking collection, or to perform incremental garbage collection.
System.GC.CollectGarbageCollector.CollectIncremental