←All patterns
Optimization

Object Pooling

Recycle a fixed set of objects instead of creating and destroying them, so spawning never stalls the frame.

What Is It?

Every Instantiate allocates managed and native memory, runs Awake/OnEnable, and every Destroy leaves garbage behind. Do that for twenty bullets a second and the garbage collector eventually wakes up mid-fight and freezes the frame — the dreaded GC spike.

An object pool pre-creates (or lazily creates) instances and keeps the inactive ones on a shelf. "Spawning" becomes pool.Get() — reactivate, reposition, go. "Destroying" becomes pool.Release(obj) — deactivate and put it back. After warm-up, the steady state allocates nothing.

Since Unity 2021 the engine ships UnityEngine.Pool.ObjectPool<T>, which takes four callbacks (create, on-get, on-release, on-destroy) plus a default capacity and max size. A turret, a particle burst, floating damage numbers or enemy waves can all be pooled with a dozen lines.

When Is It Used?

Use it for anything spawned and despawned frequently: projectiles, hit effects, pickups, audio one-shots, UI list items, enemies in wave shooters.

It matters most on mobile and consoles, where GC pauses and instantiate cost are most visible, and for objects with expensive setup (many components, large meshes).

Do not pool objects that are created once per level, or whose state is hard to reset cleanly. A pool that hands out half-reset objects causes bugs that are worse than the allocation it saved.

Interactive Demo

Click the controls and watch the objects collaborate. The console mirrors what the C# code below would log.

Object Pooling Pattern
Instantiate0Destroy0active0pooled0GC alloc0 KB
Turret
frame msGC ×0
garbage
ObjectPool<Bullet> · 0 inactive
shots: 0
Console
Press a button to see what happens…

Code

Assets / Scripts / Optimization/ ObjectPooling ›Turret.cs
using UnityEngine;

namespace Patterns.Optimization.ObjectPooling
{
    /// <summary>
    /// Fires bullets on a timer. It asks the pool for bullets instead of
    /// calling Instantiate, so firing allocates nothing after warm-up.
    /// </summary>
    public class Turret : MonoBehaviour
    {
        [SerializeField] private BulletPool pool;
        [SerializeField] private Transform muzzle;
        [SerializeField] private float fireRate = 8f;
        [SerializeField] private float bulletSpeed = 25f;

        private float cooldown;

        private void Update()
        {
            cooldown -= Time.deltaTime;
            if (cooldown > 0f || !Input.GetButton("Fire1"))
                return;

            cooldown = 1f / fireRate;
            Fire();
        }

        private void Fire()
        {
            Bullet bullet = pool.Get();
            bullet.transform.SetPositionAndRotation(muzzle.position, muzzle.rotation);
            bullet.Launch(muzzle.forward * bulletSpeed);
        }
    }
}
3 files · namespace Patterns.Optimization.ObjectPoolingC# · UTF-8 · LF

Before & After

Real-world smell: a turret that instantiates and destroys every bullet. Here is the code without the pattern, the refactored version, and the exact diff between them.

! What’s wrong with it?

Turret.Fire calls Instantiate for every shot and Destroy(bullet.gameObject, bulletLifetime) to clean up. At eight shots a second, that is a new GameObject, Rigidbody and set of components every 125 ms.

Each spawn allocates, each destroy leaves garbage, and sooner or later the garbage collector runs in the middle of a firefight and the frame stalls. The Profiler shows a GC spike that seems to have nothing to do with the code you are looking at.

✓ What changed

  • The bulletPrefab and bulletLifetime fields are replaced by a single BulletPool reference.
  • Fire calls pool.Get() and repositions the bullet instead of calling Instantiate.
  • bullet.Launch resets the bullet's state, and the bullet releases itself back to the pool on hit or timeout instead of being destroyed.
  • The pool is pre-warmed in BulletPool.Awake, so once loading is done the turret allocates nothing.
Turret.Before.cs → Turret.After.cs+6/−7
11 using UnityEngine;
22
33 namespace Patterns.Optimization.ObjectPooling
44 {
55 /// <summary>
6− /// Without pooling: every shot instantiates a fresh bullet and schedules
7− /// it for destruction, so firing allocates and leaves garbage behind.
6+ /// With pooling: the turret asks the pool for a bullet and the bullet
7+ /// returns itself, so firing allocates nothing after warm-up.
88 /// </summary>
99 public class Turret : MonoBehaviour
1010 {
11− [SerializeField] private Rigidbody bulletPrefab;
11+ [SerializeField] private BulletPool pool;
1212 [SerializeField] private Transform muzzle;
1313 [SerializeField] private float fireRate = 8f;
1414 [SerializeField] private float bulletSpeed = 25f;
15− [SerializeField] private float bulletLifetime = 2f;
1615
1716 private float cooldown;
1817
1918 private void Update()
2019 {
2120 cooldown -= Time.deltaTime;
2221 if (cooldown > 0f || !Input.GetButton("Fire1"))
2322 return;
2423
2524 cooldown = 1f / fireRate;
2625 Fire();
2726 }
2827
2928 private void Fire()
3029 {
31− Rigidbody bullet = Instantiate(bulletPrefab, muzzle.position, muzzle.rotation);
32− bullet.linearVelocity = muzzle.forward * bulletSpeed;
33− Destroy(bullet.gameObject, bulletLifetime);
30+ Bullet bullet = pool.Get();
31+ bullet.transform.SetPositionAndRotation(muzzle.position, muzzle.rotation);
32+ bullet.Launch(muzzle.forward * bulletSpeed);
3433 }
3534 }
3635 }

Advantages & Disadvantages

+ Advantages

  • Eliminates per-spawn allocation, so the GC stays quiet during gameplay.
  • Spawning is just SetActive(true) — much cheaper than Instantiate.
  • Pre-warming moves the creation cost to a loading screen where nobody notices.

− Disadvantages

  • Pooled objects hold memory even when unused; an oversized pool wastes RAM.
  • Every object must fully reset its state on reuse — velocity, health, trails, coroutines.
  • Releasing the same object twice, or using it after release, causes confusing bugs.

Tips

  1. 01Prefer the built-in ObjectPool<T> with collectionCheck: true in development; it throws on double release.
  2. 02Reset state in the on-get callback (or an OnEnable), not in the object's constructor-like Awake, since Awake only runs once.
  3. 03Clear TrailRenderers with Clear() and stop particle systems when releasing, or ghosts appear on the next spawn.
  4. 04Give the pooled object a reference back to its pool (or an Action<T> release callback) so it can return itself on hit or timeout.
  5. 05Profile with the Memory Profiler and the GC Alloc column in the CPU Profiler to confirm the pool actually removed allocations.