liveBook · Manning

Chapter 3: Memory allocations and performance

What you will learn in chapter 3 of High Performance Unity Game Development

The introduction explains why memory management matters to game performance and identifies garbage collection and fragmentation as risks explored later in the chapter.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why memory management matters to game performance and identifies garbage collection and fragmentation as risks explored later in the chapter.

Key terms: memory management, garbage-collector triggers, memory fragmentation, out-of-memory crashes

3.1 Memory fragmentation

Explains how holes in heap memory can prevent an allocation despite sufficient total free space, and how allocation order can reduce fragmentation.

Key terms: memory fragmentation, heap memory, holes, contiguous memory, heap allocations, stack allocations, reverse order, Last-In-First-Out, garbage collector

3.2 Avoiding garbage collector related slowdowns

Explains how Unity’s garbage collector reclaims heap memory and why its timing, CPU cost, and lack of compaction can affect gameplay.

Key terms: garbage collector, heap memory, garbage collection passes, CPU time, framerate, memory fragmentation, Memory Compaction, Unity GC

3.2.1 GC Issue #1 — memory isn’t freed immediately

This section explains why Unity cannot immediately reuse heap memory after objects are destroyed or abandoned, and shows the resulting allocations in the Unity Profiler.

Key terms: GameObject, Destroy(), heap, GC, cache misses, array, stack, Update(), Unity Profiler, GC allocation

3.2.2 GC issue #2 — performance

Garbage collection can cause unpredictable frame-rate drops even when Unity spreads its work across frames.

Key terms: GC, frame rate, FPS, stuttering

3.2.3 GC issue #3 — memory fragmentation

This subsection explains how delayed garbage collection and repeated allocations can fragment memory and lead to out-of-memory failures.

Key terms: Garbage Collector, memory fragmentation, GameObject

3.2.4 GC best practices

This section gives practical ways to reduce garbage collection and schedule unavoidable collection when it will affect players least.

Key terms: Garbage Collector, stack, temporary allocations, System.GC.Collect(), loading screen, object pools, recycle used objects

3.3 Avoiding fragmented memory and garbage collection with object pools

The section explains why a DOD-based Survivor game still needs Unity GameObjects to display enemies, setting up a later object-pooling example.

Key terms: DOD, Unity GameObjects

3.3.1 Common causes of memory fragmentation

The section explains why repeatedly instantiating and destroying enemy GameObjects can cause memory fragmentation and garbage collector slowdowns.

Key terms: GameObject, instantiate, destroy, heap, memory fragmentation, garbage collector slowdowns, DestroyEnemy(), DestroyImmediate(), garbage collector, deallocated

3.3.2 Using object pools to avoid memory fragmentation and garbage collection

This section shows how to allocate a level’s enemy GameObjects together, reuse them during play, and destroy them when the level ends to avoid repeated allocations and garbage-collection slowdowns.

Key terms: AllocateEnemies(), DeallocateEnemies(), pool, active false, GetFreeEnemy(), active true, memory fragmentation, garbage collector

3.3.3 What to do when object pools run out of room

Explains why object pools should not grow when full, how to set a safe capacity, and what to do when that capacity is reached.

Key terms: object pool, array, List, garbage collected, pool size, game designers, minimum target device, spawn cap, wave budget, log

3.3.4 Object pools in data-oriented design

This section shows how fixed-size data arrays and GameObject pools limit allocation problems, then separates enemy occupancy from visibility to prevent pooling bugs.

Key terms: Data-Oriented Design, pool, data arrays, GameObject pool, memory fragmentation, garbage collector, GameObject’s active boolean, object pool, alive, paused

3.3.5 Common bugs caused by object pools

This section explains two bugs caused by reusing pooled enemies: stale gameplay data and scripts that expect Unity’s Start() to run again.

Key terms: object pools, TimeToMove, gameplay data, GameObject, Start(), deactivate, reactivate

3.4 Ideal memory allocations

The section describes an allocation pattern that keeps common game data in memory, loads level-specific data when needed, and avoids new heap allocations during gameplay.

Key terms: common data, level data, object pools, memory fragmentation, Main Menu memory usage, memory leaks, baseline, out-of-memory issues, heap memory, garbage collector

3.5 Conclusion

The section concludes that DOD performance depends on allocation timing as well as data structure, and recommends pooling and allocating before gameplay to avoid runtime disruptions.

Key terms: DOD, memory fragmentation, garbage collector, object pools, level loading, heap memory

3.6 Summary

The section summarizes how fragmentation, garbage collection, object pools, and allocation timing affect memory use and gameplay performance.

Where this is used later

Concepts explained in this chapter

← Chapter 2: Structuring data for performance Chapter 4: Data and logic →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.