I heard that GC is a problem in Unity. Is GC speed affected more by amount of memory to be freed or more by the number of allocations? Is it better to have fewer larger allocations or many smaller?
It really depends. A lot of people recommends, that it is better to leave GC to do its own work.
I normally don’t have problems with GC. Sometimes I manually call GC when I can, or when I know there wont be any performance issues (when i’m in the part of the game where framerate is higher than normal).
When I’m writing my scripts I try to be efficient in memory allocations, and try not to worry about GC.
The main problem is that Unity’s running on a very old version of Mono, with a somewhat outdated GC. It’s also generating a bunch of extra, unneccessary allocations due to bugs in that old Mono version.
That’s being fixed right now - the new Mono version is available for public beta. You can check here.
Don’t worry about allocations before it becomes a big problem. Using techniques like object pools to reduce the number of allocations makes your code more complex, which makes it harder to read and more likely to contain bugs. If you’re getting spikes due to gc collects, first check if you can reduce the number/size of assets in memory before you start employing tricks.