Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. The 2022.2 beta is now available for testing. To find out what's new, have a look at our 2022.2 feature highlights.
    Dismiss Notice
  3. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  5. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice
Dismiss Notice
Submit bug reports tagged with #Beta2022Win_Unity when you encounter unknown issues while testing the 2022.2 betas for a chance to win a year of Unity Pro. For more information, have a look at our Beta Sweepstakes Announcement.

Memory leak dependent on how you destroy an object

Discussion in 'Profiler Previews' started by roberto_sc, May 5, 2020.

  1. roberto_sc

    roberto_sc

    Joined:
    Dec 13, 2010
    Posts:
    144
    If you have a reference to a MonoBehaviour and you call Destroy or DestroyImmediate on it or on its game object it will leak an empty shell .NET object.

    The same doesn't happen if you manually delete the game object via editor hierarchy. In this case the .NET object is deleted and GCed. Why does this happen?

    This is important because it shows that there's a way to completely remove an object from memory, it feels like I'm doing something wrong in the code.
     
  2. alexeyzakharov

    alexeyzakharov

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    450
    Do you set the reference to null? More likely it is that there is something referencing MonoBehaviour and preventing GC from picking up the shell. In complex cases Memory Profiler usually helps to determine the class/object which holds the reference.
     
  3. roberto_sc

    roberto_sc

    Joined:
    Dec 13, 2010
    Posts:
    144
    I don't set the reference to null, that's the point: I'm aware that having a reference to it will prevent the object to be collected, my question is why manually deleting the game object in the editor will make the object to be GCed, why is there a different behavior?

    I see why my post is misleading, I asked for "a way to completely remove an object from memory" and you correctly answered that I should set the references to null; so please let me detail my problem further: I'm in a fairly big project with thousands of objects being leaked (destroyed game objects components that should be GCed). We are doing a huge amount of work to properly fix these leakings, but when I saw that destroying the GO manually would free the empty shell object it got me thinking if there's a way to free all this memory without having to fix the code.
     
unityunity