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?
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
Anything that allocates memory on the heap will contribute to your GC problem. The only real solution is to use the Profiler to find all the allocations in your code and get rid of them if possible.
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?
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.
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?