Unity3D and C# :Memory Management and Garbage Collection
Introduction
As defined on Memorymanagement.org:
"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:
- Memory management hardware (MMUs, RAM, etc.);
- Operating system memory management (virtual memory, protection);
- 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;
03.
04.public class DestroyBasic : MonoBehaviour
05.{
06. float timeDelay=3;
07. void Update ()
08. {
09. if(Input.GetKey(KeyCode.Space))
10. {
11.
12. //Under UnityEngine Namespace
13. Destroy(gameObject);
14. //With Time Delay
15. Destroy(gameObject, timeDelay);
16. }
17. }
18.}
DestroyComponent
01.using UnityEngine;
02.
03.using System.Collections;
04.
05.public class DestroyComponent : MonoBehaviour
06. {
07. void Update ()
08. {
09. if(Input.GetKey(KeyCode.Space))
10. {
11. Destroy(GetComponent<MeshRenderer>());
12. }
13. }
14.}
Destroy is fine... How about DestroyImmediate? Which one to use?
DestroyImmediate
Destroys the object obj
immediately. You are strongly recommended to use Destroy instead.
http://docs.unity3d.com/Documentation/ScriptReference/Object.DestroyImmediate.html, 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 https://docs.unity3d.com/Documentation/Manual/UnderstandingAutomaticMemoryManagement.html
Garbage Collection
Read: https://docs.unity3d.com/Documentation/Manual/UnderstandingAutomaticMemoryManagement.html
Further reading on Automatic Memory Management : http://bit.ly/1gRja5l
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
References
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.