Search Unity

List of things that cause Garbage Collection?

Discussion in 'Scripting' started by infinitypbr, Oct 22, 2014.

  1. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I'm having a garbage issue. My game is pretty large at this point, lots of scripts, so no one thing is causing it, but every maybe 3-5 seconds there's a spike in the profiler from garbage collection. I'd like to minimize this, so I'm assuming I need to change some scripts?

    I've heard that instantiate and destroy both create garbage. What other things are likely to contribute to the garbage collection process?

    Thanks!
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    There are a few things... some create small amounts, some more... few examples off teh top of my head.

    foreach
    transform.name (wtf.. really? yep).. I believe there are a handful of others that also create garbage when used.
    Destroy
    I think some of the GetComponent functions create some..
    anything that creates a list
     
  3. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,633
    Last edited: Oct 23, 2014
  4. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    I'll check out the link!
     
  5. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Followup! My MiniMap camera is using 11.5kb per frame in GC. It goes down to zero when I deactivate "Edge Detect Effect Normals", one of the pro image scripts. Is it possible to edit that script to reduce or remove the GC?

    Edit: Depth of Field 34 on my main camera is using over 100kb per frame. I'm guessing that's more of a problem than 10. But again, is this a script I should edit?
     
  6. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,633
    11.5kb is a lot, 100kb is insane. Yes, you can try editing the scripts, but I'd make a copy so it doesn't get overwritten should you update them. No guarantee you'll be able to eliminate the allocations though.
     
    Last edited: Oct 23, 2014
  7. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    That script is certainly beyond me. Best guess maybe it's creating and removing textures?

    But, changing it for the "Depth of Field Scatter" script provides similar results without the garbage.
     
  8. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,633
    I just did a quick test with Depth of Field 34 and it's not showing any GC in the profiler for me. I looked at the code and it's creating textures using RenderTexture.GetTemporary and releasing at the end with RenderTexture.ReleaseTemporary. This shouldn't be allocating any new memory. Perhaps it's some settings or maybe you're using an older version of the script?
     
  9. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Could be an older version -- although I'm running 4.6 b20, but i guess that doesn't mean it updates the assets for each project huh.
     
    Last edited: Oct 23, 2014
  10. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,633
    Nope. It updates the Unitypackage files that they're installed from, but not the files in the projects.
     
    infinitypbr likes this.