Skip to content

Instantly share code, notes, and snippets.

Unity Memory Optimization: The Buffer Pattern

This Gist demonstrates the difference between High-Allocation and Zero-Allocation patterns in Unity.

The Problem: Garbage Collection (GC) Stutter

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.

The Solution: Pre-allocation & Buffers

By declaring your collections (Lists, Arrays, Dictionaries) as class members and using .Clear() instead of new, you maintain a flat memory footprint.

Lerp vs SmoothDamp — Unity

The Problem

Vector3.Lerp with a fixed t in Update() is NOT smooth movement. It approaches the target exponentially — slowing forever, never truly arriving.

The Solution

Vector3.SmoothDamp accelerates toward the target and decelerates on arrival. It actually reaches the destination with physically believable motion.

@Anmol-1903
Anmol-1903 / 0_Unity_Object_Pooling.md
Last active March 18, 2026 04:09
Unity Object Pooling - Naive vs Pooled

Object Pooling vs Instantiate/Destroy — Unity

A simple example showing why Object Pooling is preferred over Instantiate/Destroy for frequently spawned objects in Unity.

The Problem

Calling Instantiate and Destroy repeatedly causes:

  • Memory allocations every spawn
  • Garbage Collector spikes
  • Frame drops and stutters (critical in VR)