Object Pooling
Reuse, don't reallocate
Object pooling pre-allocates and reuses game objects instead of creating and destroying them, avoiding memory fragmentation and allocation overhead.
Overview
Creating and destroying objects during gameplay causes problems: memory allocation takes time, and frequent allocation/deallocation fragments memory. Object pooling solves this by pre-allocating a fixed number of objects and recycling them. Bullets don’t get created—they’re borrowed from a pool. When finished, they return to the pool rather than being destroyed.
Fast facts
- Problem solved: allocation overhead and fragmentation.
- Solution: pre-allocate and recycle objects.
- Common uses: bullets, particles, enemies.
- Implementation: array of pre-created objects, active flag.
- Tradeoff: fixed maximum count, slightly complex management.
How it works
Object pooling pattern:
- Initialisation: create pool of objects at startup.
- Request: mark object active, return reference.
- Use: treat as normal object.
- Release: mark inactive, stays in pool.
- Reuse: next request uses same object.
When to use
Object pooling benefits:
- Frequent creation: many short-lived objects.
- Predictable count: known maximum needed.
- Performance critical: allocation pauses unacceptable.
- Constrained memory: fragmentation dangerous.