This Gist demonstrates the difference between High-Allocation and Zero-Allocation patterns in Unity.
When you use the new keyword inside Update(), LateUpdate(), or FixedUpdate(), you are allocating memory on the Managed Heap. When the heap is full, Unity must pause execution to collect the "garbage," leading to micro-stutters and frame drops.
By declaring your collections (Lists, Arrays, Dictionaries) as class members and using .Clear() instead of new, you maintain a flat memory footprint.
- Attach the script to any GameObject.
- Open the Unity Profiler (Ctrl+7).
- Look at the CPU Usage module.
- Compare the GC Alloc column between the
RunBadWayandRunGoodWaymethods.
Shared as part of a technical deep-dive on LinkedIn.