Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Configuring garbage collection

Set up the garbage collector in your project.
Read time 3 minutesLast updated 13 days ago

The garbage collection mode can be configured in the following ways:

Disabling incremental garbage collection

To disable incremental garbage collection:
  1. Open the Project Settings window (Edit >
    Project Settings

    ?

    )
  2. Navigate to the Configuration settings (Player > Other Settings > Configuration)
  3. Disable the Use Incremental GC checkbox.
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.

Using the GarbageCollector API

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:
Note
The
GarbageCollector
API isn't supported for the CoreCLR scritping back end.

Disabling the garbage collector

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.

Manually run the garbage collector

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:

Additional resources