Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by Matt 11 · Jul 14, 2010 at 06:03 PM · objectprofilercount

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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 &lt; renderer.materials.Length; ++i)
     //{
     //    DestroyImmediate(renderer.materials[i]);
     //}

 //LEAK
     //Material material = renderer.material;
     //for (int i = 0; i &lt; 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);

}

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matt 11 · Jul 19, 2010 at 01:23 PM 0
Share

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?

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Oct 27, 2010 at 12:29 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Count >= then object tag becomes active 1 Answer

Profiler: GameObjects in Scene count? 1 Answer

Counting active objects in a list 3 Answers

What is "mesh count"? 0 Answers

Profiler(Show related Object) show 'N/A' object name 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges