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.


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);

void Update()
    Renderer renderer = objectCopy3.GetComponent<Renderer>();

        //Material material = renderer.material;

        //Material material = renderer.materials[0];
        //for (int i = 0; i < renderer.materials.Length; ++i)
        //    DestroyImmediate(renderer.materials*);*
 *//Material material = renderer.material;*
 *//for (int i = 0; i < renderer.materials.Length; ++i)*
 _//    DestroyImmediate(renderer.materials*);*_
 _*//Material material = renderer.materials[0];*_
 _*//Material material = renderer.materials[0];*_
 _*objectCopy3 = (GameObject)Instantiate(objectCopy2);*_

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.

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); `