Building Allocation-Free Spawners in Unity
When building a procedural generation system for Unity, the biggest performance trap is garbage collection. Every new keyword in a hot path creates heap allocations that the GC must eventually collect, causing frame hitches.
In the Procedural Generator, we took a zero-allocation approach:
Object Pools. Instead of instantiating and destroying GameObjects, we pre-allocate a pool during scene load. The pool grows on demand but never shrinks, so after warm-up there are zero allocations.
Struct Configs. All spawner configurations use structs instead of classes. Structs live on the stack and are copied by value — no heap allocations, no GC pressure.
NativeArray for Positions. For position calculations, we use Unity's NativeArray with the Jobs system. This keeps data cache-friendly and avoids managed heap allocations entirely.
The result: our spawner can process 10,000 entities per frame at 60fps on a mid-range mobile device with zero GC allocations after the initial warm-up frame.