Our application seems to be suffering from serious garbage collector spikes in the profiler every few frames. The spikes are definitely being caused by OnGUI functions.
As long as there is a single OnGUI overload in the scene, the spikes will appear. They will drop the fps sometimes by 90% for a single frame. There’s no noticeable difference if there is something inside the OnGUI method, or if it’s empty. As long as it’s there the spikes happen.
Adding more scripts with overloaded OnGUI will cause spikes to happen more often.
Most of the time spent during those spikes are GC.Collect calls (generally from GUI.Repaint->BeginGUI).
This happens with both Unity 2 or 3.
Simple experiment
Scene with 1 cube and 1 camera, with the following script attached to the cube GameObject:
using UnityEngine;
using System.Collections;
public class OnGUITest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
}
}
Now lets try that same scene, but with the OnGUI function commented out:
using UnityEngine;
using System.Collections;
public class OnGUITest : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//void OnGUI()
//{
//}
}
You can see how this can cause a lot of problems in a larger scene where we try to maintain an fps of 60, but the spikes happen every few frames and lower it to 15-20, which is very noticeable. (And if you have multiple overridden OnGUI methods, they are even more common)
I understand that GC needs to do its job, but correct me if I’m wrong, such huge spikes shouldn’t be caused by an empty overload of OnGUI function.
So my question is: Any way to get around this? Other than using 3rd party GUI system.