Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
2
Question by Thejon · Nov 13, 2013 at 01:21 AM · c#gameobjectmemory-leak

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);
     }
 }
Comment
Add comment · Show 5
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 rutter · Nov 13, 2013 at 01:34 AM 1
Share

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().

avatar image Eric5h5 · Nov 13, 2013 at 01:59 AM 0
Share

Garbage collection isn't relevant for Unity objects.

avatar image Thejon · Nov 13, 2013 at 07:09 PM 1
Share

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);
     }
 }
avatar image Kiloblargh · Nov 13, 2013 at 07:20 PM 0
Share

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.

avatar image Thejon · Nov 13, 2013 at 07:25 PM 0
Share

$$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

memoryleak.zip (121.4 kB)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · 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 Thejon · Nov 13, 2013 at 07:02 PM 0
Share

Resources.UnloadUnusedAssets did not make any difference, although I didn't expect it to because I wasn't using Resources.Load either.

avatar image Julien-Lynge · Nov 13, 2013 at 07:05 PM 0
Share

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.

avatar image Thejon · Nov 13, 2013 at 07:30 PM 0
Share

There are no materials or textures in the project. This is a test with empty game objects.

avatar image Eric5h5 · Nov 13, 2013 at 09:06 PM 0
Share

UnloadUnusedAssets doesn't have anything to do with Resources.Load. See the docs.

avatar image
-3

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.

Comment
Add comment · Show 2 · 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 Eric5h5 · Jul 15, 2016 at 05:59 AM 1
Share

That's the exact same thing. There is only one Destroy function.

avatar image Bunny83 Eric5h5 · Jul 15, 2016 at 10:14 AM 0
Share

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

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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