- Home /
Is Destroy (gameobject) leaking memory?
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[i] );
}
_instances.Clear();
_display = string.Format("{0} objects", _instances.Count);
}
}
I'd normally suspect lingering object references that prevent collection, but it looks to me like you're handling your end with that Clear()
call. You might try to force garbage collection between checks with System.GC.Collect()
.
I created another test project with two scenes. One creates a bunch of empty gameobjects (this time from a referenced prefab, just in case there is a problem with using the GameObject constructor). The other has calls to unload unused assets and call the garbage collector, before reloading the first scene. If it helps, I am using Unity 4.2.2 and Xcode 5.0 building to an iPad2.
The first scene builds up 280 $$anonymous$$B + of memory, then swapping scenes only frees up about 20 $$anonymous$$B or so. Then the memory pressure immediately rises up again until the app finally crashes. Here are the scripts, each attached to the camera in an otherwise blank scene.
using UnityEngine;
using System.Collections;
public class $$anonymous$$emoryLeakScript : $$anonymous$$onoBehaviour
{
const int addCount = 3000;
const int maxCount = 520000;
public GameObject emptyPrefab;
int count = 0;
void Update ()
{
Create();
if (count >= maxCount)
{
Reload();
}
}
void Create ()
{
for (int i = 0; i < addCount; ++i) {
Instantiate( emptyPrefab );
}
count += addCount;
}
void Reload ()
{
Application.LoadLevel(1);
}
}
and...
using UnityEngine;
using System.Collections;
public class Clear$$anonymous$$emoryScript : $$anonymous$$onoBehaviour
{
void Start ()
{
Resources.UnloadUnusedAssets();
System.GC.Collect();
Invoke("Reload", 3);
}
void Reload ()
{
Application.LoadLevel(0);
}
}
This isn't a solution to the garbage collection in Unity sucking, but maybe a workaround, try ins$$anonymous$$d of reloading the level you are on, loading an empty level with a script on it that redirects to the next level.
$$anonymous$$iloblargh, sorry for any confusion in my post, but that is what I am doing. Here is the project in case it helps to clarify
Answer by Eric5h5 · Nov 13, 2013 at 01:59 AM
Unity objects are not part of Mono and are not subject to garbage collection. You can use Resources.UnloadUnusedAssets.
Resources.UnloadUnusedAssets did not make any difference, although I didn't expect it to because I wasn't using Resources.Load either.
Are you correctly cleaning up materials and textures? They aren't connected to gameObjects in the way you might think - removing a gameObject will not remove its materials and the textures used by those materials.
There are no materials or textures in the project. This is a test with empty game objects.
UnloadUnusedAssets doesn't have anything to do with Resources.Load. See the docs.
Answer by Fonglet · Jul 15, 2016 at 05:38 AM
So I know this is 3 years old now but I had this same problem and figured I'd answer for anyone that had the same issues as me.
You need to use Destroy() not GameObject.Destroy();
EDIT: As was pointed out below this answer is actually wrong. I changed a few things at once turns out what was actually fixing it for me was that I had to destroy my procedural generated meshes explicitly using
Destroy(gameObject.GetComponent <MeshFilter>().sharedMesh);
Destroy(gameObject.GetComponent<MeshFilter>().mesh);
sorry for the false answer, thanks for correcting me.
That's the exact same thing. There is only one Destroy function.
Exactly. Destroy() is a static method of UnityEngine.Object. So since GameObject is derived from UnityEngine.Object it's the same method. It's also the same as $$anonymous$$onoBehaviour.Destroy() or $$anonymous$$aterial.Destroy().
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Problem with taking a gameobject from gameobject list to inventory. 1 Answer
Glow single Object 1 Answer
How to SetTrigger all colliders with a specific name 1 Answer