Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


Optimizing your code for managed memory

Approaches for optimizing your code to work with managed memory.
Read time 1 minuteLast updated 6 days ago

C#’s automatic memory management reduces the risk of memory leaks and other programming errors, in comparison to other programming languages like C++, where you must manually track and free all the memory you allocate.
Automatic memory management allows you to write code quickly and with few errors. However, this convenience might have performance implications. To optimize your code for performance, you must avoid situations where your application triggers the garbage collector a lot. This section outlines some common issues and workflows that affect when your application triggers the garbage collector.

Topic

Description

Reference type managementOptimize how you use reference types in your code.
Pooling and reusing objectsReduce CPU load and garbage collector overhead by using Unity's
UnityEngine.Pool
APIs, which allow you to pool and reuse frequently-used objects rather than repeatedly creating and destroying them.
Optimizing arraysOptimize when and how you use arrays in your code.

Additional resources