I created a simple test in an empty project with an empty scene (just the one script on the default camera). The test uses a button to create 1000 empty game objects, and then a different button to destroy them. The project builds to iOS and I used Xcode’s profiler for checking the memory.
Results: (for each entry I wait 30 seconds before moving on)
- start 8.8 MB to 9.1 MB
- create 9.7 MB to 9.8 MB
- destroy 9.8 MB
- create 9.9 MB
- destroy 9.8 MB
- create 10.1 MB
- destroy 10 MB
Next I just quickly alternated create and destroy and the memory rose to 11.1 MB before I was quit. I would like to know if this was somehow expected behavior or if I did something wrong. Here is the script that I used:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MemoryLeakScript : MonoBehaviour
{
List<GameObject> _instances = new List<GameObject>();
string _display = "";
void OnGUI ()
{
if (GUI.Button( new Rect(10, 10, 100, 30), "Create"))
{
Create();
}
if (GUI.Button( new Rect(10, 50, 100, 30), "Destroy"))
{
Kill();
}
GUI.Label( new Rect(120, 10, 100, 30), _display);
}
void Create ()
{
for (int i = 0; i < 1000; ++i)
{
GameObject obj = new GameObject("dummy");
_instances.Add( obj );
}
_display = string.Format("{0} objects", _instances.Count);
}
void Kill ()
{
for (int i = _instances.Count - 1; i >= 0; --i)
{
GameObject.Destroy( _instances *);*
-
}* -
_instances.Clear();* -
_display = string.Format("{0} objects", _instances.Count);* - }*
}