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:
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:
Is this a horrible idea? Any other, better approaches?
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?
