How to stop GC.Collect() causing hickups

The profiler tells me that the sporadic hickups in my game are caused by GC.Collect() What's a good strategy to solve this?

This talk has a pretty decent section on "How to optimize for GC"

http://unity3d.com/support/resources/unite-presentations/optimizing-for-unity-iphone-1.5

Basically it boils down to:

  • minimizing per frame allocations (Watch out for the string class!)
  • call GC.Collect() when you know for sure that you can deal with a small hickup. (when the user pauses the game, during a loading screen, when opening a gui menu)