As defined on

"Memory management is the art and the process of coordinating and controlling the use of memory in a computer system.

Memory management can be divided into three areas:

  1. Memory management hardware (MMUs, RAM, etc.);
  2. Operating system memory management (virtual memory, protection);
  3. Application memory management (allocation, deallocation, garbage collection)."


Game development

Unity Supports automatic memory management, where you don't have to bother about your used assets/objects, Unity's mono engine will release the memory and will use the same for other purposes.In the past, the user has to manually allocate the memory and release it at an appropriate time, which was an overhead.

Even now you can manually remove GameObjects or components of GameObjects at runtime using Destroy Function.

01.using UnityEngine;
02.using System.Collections;
04.public class DestroyBasic : MonoBehaviour
06.  float timeDelay=3;
07.  void Update ()
08. {
09.  if(Input.GetKey(KeyCode.Space))
10.   {
12.     //Under UnityEngine Namespace
13.     Destroy(gameObject);
14.    //With Time Delay
15.    Destroy(gameObject, timeDelay);
16.  }
17. }



01.using UnityEngine;
03.using System.Collections;
05.public class DestroyComponent : MonoBehaviour
06. {
07. void Update ()
08. {
09.  if(Input.GetKey(KeyCode.Space))
10.  {
11.   Destroy(GetComponent<MeshRenderer>());
12.  }
13. }

Destroy is fine... How about DestroyImmediate? Which one to use?



Destroys the object obj immediately. You are strongly recommended to use Destroy instead., explains why.


Value and Reference Types

  • Value types are types which hold both data and memory on the same location.
  • Memory allocated on stack.
    • These include integers, floats, booleans and Unity's struct types (eg, Color andVector3).
  • A reference type has a pointer which points to the memory location.
  • Memory allocated on heap.
    • Examples of reference types include objects, strings and arrays.

As defined on


Garbage Collection


Further reading on Automatic Memory Management :

I thought of writing about Memory management in unity through C#, but nothing can beat Wendelin Reich's articles in Gamasutra about C# memory Management for unity.

See Also


There are three posts on C# memory management:

  • This first post discusses the fundamentals of memory management in the garbage-collected world of .NET and Mono. Author also discussed some common sources of memory leaks.
  • The second looks at tools for discovering memory leaks. The Unity Profiler is a formidable tool for this purpose, but it's also expensive. Author therefore discussed .NET disassemblers and the Common Intermediate Language (CIL) to show you how you can discover memory leaks with free tools only.
  • The third post discusses C# object pooling. Again, the focus is on the specific needs that arise in Unity/C# development.