Those gaps? They're why your game stutters.
By Fuat Can Köseoğlu,
This is your Unity heap at game start: ███████████████████ This is your Unity heap after 5 minutes: █░░█░█░░░░█░█░░█░█░░░█░ Those gaps? They're why your game stutters. Memory Profiler shows 1.5GB free but frame time spikes to 45ms. The problem isn't HOW MUCH memory you have - it's HOW IT'S ARRANGED. Tower defense game. Players build, enemies spawn, projectiles fly. Normal gameplay. After 5 minutes: stuttering mess. THE PROJECTILE PROBLEM Firing 1000 mixed projectiles: • Bullet (~10KB) → dies at 2 seconds • Rocket (~32KB) → dies at 1 second • Laser (~20KB) → dies at 3 seconds What happens in memory: Memory after random deaths: [Bullet][░░32KB gap░░][Rocket][░░20KB░░][Bullet][░░░░░] Need a new 32KB rocket? 1.5GB free but no contiguous 32KB block. GC struggles to find contiguous space = allocation spike. THE PERFORMANCE KILLER Fragmented memory destroys CPU cache: • Cache loads 64-byte lines • Your bullet sits alone in those 64 bytes • Result: More cache misses • Cache miss = ~200 cycles vs ~4 cycles (workload and architecture dependent) Unity makes it worse: • Instantiate() = random placement • Destroy() = random holes • GetComponent() = pointer chasing through scattered memory THE FIX Object pooling. Cache components. Batch operations. Stop: Instantiate/Destroy every frame Start: Pool and reuse Results: Stable for hours instead of minutes. Check your profiler. Now you know why those spikes exist. Note: Unity’s GC doesn’t always compact memory, so even with lots of free memory, fragmentation can cause allocation spikes. Follow for O(1) access to daily game dev tips!