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.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.