Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
CubicleNinjas's avatar
11 years ago

Garbage Collection

I was curious if anyone here is using special garbage collection techniques for better VR performance?

While staring at the Profiler I would see spikes at random times. When these occurred they were often taking up to 70% of the CPU. This was Unity's natural garbage collection approach kicking in often causing judder. Luckily, they have a sweet article on different techniques.

Here is the most relevant section:
Small heap with fast and frequent garbage collection

This strategy is often best for games that have long periods of gameplay where a smooth framerate is the main concern. A game like this will typically allocate small blocks frequently but these blocks will be in use only briefly. The typical heap size when using this strategy on iOS is about 200KB and garbage collection will take about 5ms on an iPhone 3G. If the heap increases to 1MB, the collection will take about 7ms. It can therefore be advantageous sometimes to request a garbage collection at a regular frame interval. This will generally make collections happen more often than strictly necessary but they will be processed quickly and with minimal effect on gameplay:-


Since then we've switched from relying on the automatic approach to cleaning house every 74 frames. I find this approach almost fully removes the garbage collection spikes from our projects. Our upcoming build of Guided Meditation will have this (and a billion other enhancements).

Below is the code I'm using if helpful. Save this as "GarbageCollectionManager.cs" in your project. The frame frequency can be modified to your liking in the inspector:

using UnityEngine;

class GarbageCollectionManager : MonoBehaviour {
public int frameFreq = 75;
void Update() {
if (Time.frameCount % frameFreq == 0)
System.GC.Collect();
}
}


Is this a horrible idea? Any other, better approaches?

3 Replies

Replies have been turned off for this discussion
  • "CubicleNinjas" wrote:
    I would see spikes at random times. When these occurred they were often taking up to 70% of the CPU.

    If GC is taking that much time, scripts are probably making excessive allocs and frees. I've seen plenty of cases where logging or texture management was quietly re-allocating huge buffers every Update. You might want to do some memory profiling and refactor a little to avoid those.
  • I just watched this video and these guys talk a about the garbage collector at around 12 minutes and again at the end during q & a about a function called Resources.UnloadUnusedAsset that is a kind of quick memory cleanup. It's a good video even though it's a bit old



    http://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html

    The first 20 minutes is all about memory management and other good stuff. Thanks again for backing my Indiegogo campaign, I hope that helps.

    edit: I was kind of reading about that a little more and found this which also looks good http://supersegfault.com/managing-memory-in-unity3d/
  • Super helpful. Thanks Dave and Molton!

    It turned out to be (Dave called it) script and texture related. Spending some time with Profiler helped smooth everything out. Still using this GC method though as it seems to help for our app. Much appreciated!