- Home /
unity profiler total object count regarding renderer material memory leak
What exactly is the Total Object Count in unity's profiler keeping track of? If I make a simple scene (no main camera) that has 1 game object with a MonoBehaviour script attached (running no code), and I run the scene, then select my game object in the scene hierarchy, the Total Object Count increases by 10. Then, when I deselect my game object in the scene hierarchy, the Total Object Count decreases by 7 (a net increase of 3). If I keep doing this over and over again, my Total Object Count will just keep increasing indefinitely as far as I can tell. The documentation for the profiler states:
"Object Count is the total number of Objects that are created. If this number rises over time then it means your game is creating some objects that are never destroyed."
The entire reason I made this small project was to try and track down some memory leak issues in my current unity project. Maybe if I understood what the Total Object Count field was being used for and what the expected behavior is, I could use it more effectively as a debugging tool.
Thanks.
Answer by Matt 11 · Jul 16, 2010 at 06:54 PM
Ok, so I found the cause of our problem and a sort-of work around. It basically stems from accessing a material on a renderer by making a call: Renderer.material or Renderer.materials[0]. When you access the material on a renderer in either of these ways, the object count in the unity editor seems to increase. If you call DestroyImmediate on the gameObject this renderer is attached to, the object count does NOT go down. Instead, you MUST call DestroyImmediate on Renderer.material and THEN call DestroyImmediate on the gameObject to clear up all object counts.
The following code snippit reproduces the problem (and offers a variety of solutions / possible bugs). This script assumes you attach it to a gameObject in your scene and supple the public variable "publicObject" with a gameObject with a Renderer Component with a Material. Let me know if there are any questions or if something is unclear!
======================================================================================
using UnityEngine; using System.Collections;
public class DestroyTest : MonoBehaviour {
GameObject objectCopy1 = null; GameObject objectCopy2 = null; GameObject objectCopy3 = null;
public GameObject publicObject = null;
// Use this for initialization void Start() { objectCopy1 = (GameObject) Instantiate(publicObject); objectCopy1.renderer.material.mainTextureScale = Vector2.zero; objectCopy1.active = false;
objectCopy2 = (GameObject)Instantiate(objectCopy1);
objectCopy2.active = false;
objectCopy3 = (GameObject)Instantiate(objectCopy2);
objectCopy3.SetActiveRecursively(true);
}
void Update() { Renderer renderer = objectCopy3.GetComponent<Renderer>();
//WORKS
//Material material = renderer.material;
//DestroyImmediate(renderer.materials[0]);
//LEAK
//Material material = renderer.materials[0];
//for (int i = 0; i < renderer.materials.Length; ++i)
//{
// DestroyImmediate(renderer.materials[i]);
//}
//LEAK
//Material material = renderer.material;
//for (int i = 0; i < renderer.materials.Length; ++i)
//{
// DestroyImmediate(renderer.materials[i]);
//}
//WORKS
//Material material = renderer.materials[0];
//DestroyImmediate(renderer.materials[0]);
//WORKS
//Material material = renderer.materials[0];
//DestroyImmediate(renderer.material);
DestroyImmediate(objectCopy3);
objectCopy3 = (GameObject)Instantiate(objectCopy2);
}
}
Answer by Joachim Ante · Jul 19, 2010 at 10:27 AM
This is expected behaviour. Usually the object count will go down when you call, Resources. CollectUnusedAssets or load another level.
Essentially Unity is not ref counted for assets but usesgarbage collection when loading levels or when calling Resources.CollectUnusedAssets explicitly.
So if you contionously create objects, then call renderer.material on it (which creates an instance of the material, as opposed to renderer.sharedMaterial which does not create an instance) and then don't delete them, thats a leak. You can either destroy the material when destroying the object, or call Resources.CollectUnusedAssets() once in a while.
calling renderer.material should only be done if you are actually modifying the material, if you don't just call renderer.sharedMaterial and you will have no materials created behind the scenes thus no leaks.
Interesting. I understand now, although I'm a bit confused about the sample code I pasted above.
//LEA$$anonymous$$
//$$anonymous$$aterial material = renderer.materials[0];
//for (int i = 0; i < renderer.materials.Length; ++i)
//{
// DestroyImmediate(renderer.materials[i]);
//}
//LEA$$anonymous$$
//$$anonymous$$aterial material = renderer.material;
//for (int i = 0; i < renderer.materials.Length; ++i)
//{
// DestroyImmediate(renderer.materials[i]);
//}
Shouldn't these two strategies for deleting rendering materials result in no leak as well?
Answer by Joachim Ante · Jul 20, 2010 at 03:05 PM
The leak in the posted code is because you are continously calling renderer.materials. Thus recreating instantiated materials again and again after destroying them. Best approach is:
Material[] materials = renderer.materials; foreach (Material m in materials) DestroyImmediate(m);
I want to procedurally add items to the scene from an Editor class that implements a new editor item. I add a new game object, add a $$anonymous$$eshFilter and a $$anonymous$$eshRenderer, create a new $$anonymous$$aterial, and attach the new material to the renderer.
This all works, but if I delete my added game object and save the scene I get a warning about leaking a $$anonymous$$aterial. The message repeats each time I save the scene. Is there a way to avoid this? Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613064750im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Profiler: GameObjects in Scene count? 1 Answer
Counting active objects in a list 3 Answers
What is "mesh count"? 0 Answers
How to Format Timer Counting Up from Zero? 0 Answers
Profiler(Show related Object) show 'N/A' object name 0 Answers