Chapter 3: Memory allocations and performance
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
- Memory Management Performance Risks. Explain how poor memory management can disrupt a game's smooth, high-frame-rate experience, including performance drops associated with garbage-collector triggers and possible out-of-memory crashes associated with fragmentation.
- Heap fragmentation and contiguous space. Explain how deallocating heap objects can leave separate holes and why a new object may fail to allocate even when their combined free space is large enough. Use the section’s three-array example to illustrate the difference between total free memory and the largest available hole.
- Garbage collection and delayed reuse. Trace how the garbage collector finds unused objects, marks them for deletion, and later removes them so their memory can be reused. Explain that memory is not necessarily freed as soon as an object becomes unused.
- Unpredictable gameplay performance drops. Explain why Unity’s automatic garbage collection can cause a sudden gameplay slowdown: collection uses CPU time and may run during gameplay, especially noticeably when the device or CPU is already under strain.
- Noncompacting memory fragmentation. Explain that Unity’s garbage collector does not compact freed memory, so collection can leave memory fragmented. Distinguish this from collectors that move memory to compact it, noting that such movement is costly.
- Delayed heap memory reuse. Explain why destroying an enemy GameObject does not let a newly instantiated enemy immediately reuse its heap memory, and describe the possible effect on memory layout.
- Temporary array allocation evidence. Describe how the temporary-array example uses the Unity Profiler to show that abandoning heap data does not immediately make its memory reusable, and state the practical implication.
- Garbage collection performance impact. Explain why garbage collection can cause stuttering in a Unity game and why its timing makes the problem especially disruptive and hard to reproduce.
- Garbage collection and fragmentation. Explain why setting data to null or destroying a GameObject does not make its memory immediately reusable, and how frequent allocation and deallocation can leave holes in memory that may eventually cause an out-of-memory failure.
- Allocation and deallocation habits. Describe the allocation habits recommended to reduce garbage collection: group related allocations during loading, deallocate in reverse allocation order, and use the stack for temporary allocations.
- Timing unavoidable garbage collection. Explain why and when the section recommends manually calling System.GC.Collect() if collection is unavoidable.
- GameObjects alongside DOD logic. Explain why the Survivor game still needs Unity GameObjects for enemies even when its game logic is written using DOD.
- Per-spawn GameObject allocation costs. Explain why instantiating an enemy GameObject when needed and destroying it when no longer needed is discouraged.
- Level-wide enemy pool lifecycle. Describe how the Survivor example creates a pool sized for a level’s enemies, starts its GameObjects inactive, and destroys them together when the level ends.
- Enemy reuse during gameplay. Trace how spawning takes an unused enemy GameObject from the pool, activates it, and later deactivates it for reuse; explain the memory-performance benefit.
- Cost of resizing pools. Explain why resizing a full object pool is the wrong response and use the C# List example to illustrate the cost.
- Enforced pool capacity limits. Describe how to choose a pool size using the level's maximum object count, rules that enforce it, and the minimum target device's capacity.
- Full pool response. Describe what happens when a spawn would exceed the pool and how developers should address the event afterward.
- Fixed Size Data Pools. Explain why DOD treats enemy data arrays as pools and allocates them alongside GameObject pools when loading a level.
- Active Flag Occupancy Bug. Trace why using a GameObject’s active boolean for both enemy occupancy and visibility breaks the pause-and-unpause behavior.
- Separate Alive Array Workflow. Describe how the Alive array and GameObject active flag are used when spawning, removing, pausing, and unpausing enemies.
- Pooled gameplay data reset. Trace how a reused enemy can inherit stale gameplay data, and explain why every pooled slot must be initialized when it is reused.
- Pooled GameObject lifecycle pitfall. Explain why a script that shows a spawn effect in Start() can work the first time a pooled enemy appears but fail when that GameObject is reused.
- Common and level allocation lifecycle. Trace when common and level-specific data are allocated and released, and explain how that arrangement avoids holes in common data.
- Gameplay allocation avoidance benefits. Explain why allocating needed memory during loading and reusing it during gameplay helps avoid performance drops and makes out-of-memory failures easier to diagnose.
- Allocation timing and performance. Explain why when data is allocated and deallocated matters in DOD: poor timing can fragment memory or trigger garbage collection during play, hurting the user experience.
- Pooling and loading allocations. Describe the recommended allocation pattern: use object pools to avoid fragmentation and garbage-collector triggers, and ideally allocate a level’s needed memory during loading rather than allocate or deallocate heap memory while it runs.
What it builds on
- The heap (chapter 2) · Assumes the distinction between heap memory and other memory storage.
- The stack (chapter 2) · Assumes the stack’s reverse-order deallocation behavior.
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.
- Memory Management Performance Risks: Explain how poor memory management can disrupt a game's smooth, high-frame-rate experience, including performance drops associated with garbage-collector triggers and possible out-of-memory crashes associated with fragmentation.
Why it matters: These risks explain why memory allocation choices matter to a game's responsiveness and reliability.
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.
- Heap fragmentation and contiguous space: Explain how deallocating heap objects can leave separate holes and why a new object may fail to allocate even when their combined free space is large enough. Use the section’s three-array example to illustrate the difference between total free memory and the largest available hole.
Why it matters: A free-memory total can look sufficient while no single space can hold the next allocation. - Stack-like heap allocation ordering: Describe how grouping heap data by lifetime and releasing it together or in reverse allocation order can help preserve contiguous free space. Distinguish this strategy from a guarantee in Unity, where the garbage collector can still leave memory fragmented.
Why it matters: Allocation lifetime and release order can reduce fragmentation, but they are not a complete guarantee in Unity.
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.
- Garbage collection and delayed reuse: Trace how the garbage collector finds unused objects, marks them for deletion, and later removes them so their memory can be reused. Explain that memory is not necessarily freed as soon as an object becomes unused.
Why it matters: An unused object’s memory may not be available for reuse right away. - Unpredictable gameplay performance drops: Explain why Unity’s automatic garbage collection can cause a sudden gameplay slowdown: collection uses CPU time and may run during gameplay, especially noticeably when the device or CPU is already under strain.
Why it matters: A collection pause can be most noticeable when smooth gameplay matters to the player. - Noncompacting memory fragmentation: Explain that Unity’s garbage collector does not compact freed memory, so collection can leave memory fragmented. Distinguish this from collectors that move memory to compact it, noting that such movement is costly.
Why it matters: Freeing unused objects does not, by itself, prevent fragmented memory in Unity.
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.
- Delayed heap memory reuse: Explain why destroying an enemy GameObject does not let a newly instantiated enemy immediately reuse its heap memory, and describe the possible effect on memory layout.
Why it matters: Destroying objects repeatedly can increase memory use and scatter objects across the heap rather than promptly recycling their memory. - Temporary array allocation evidence: Describe how the temporary-array example uses the Unity Profiler to show that abandoning heap data does not immediately make its memory reusable, and state the practical implication.
Why it matters: The profiler result makes the cost of repeatedly creating short-lived heap data visible in a frame.
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.
- Garbage collection performance impact: Explain why garbage collection can cause stuttering in a Unity game and why its timing makes the problem especially disruptive and hard to reproduce.
Why it matters: Unpredictable frame-rate drops can interrupt gameplay and make performance problems difficult to reproduce.
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.
- Garbage collection and fragmentation: Explain why setting data to null or destroying a GameObject does not make its memory immediately reusable, and how frequent allocation and deallocation can leave holes in memory that may eventually cause an out-of-memory failure.
Why it matters: This explains why repeatedly creating and discarding objects can cause memory problems even when the objects are no longer needed.
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.
- Allocation and deallocation habits: Describe the allocation habits recommended to reduce garbage collection: group related allocations during loading, deallocate in reverse allocation order, and use the stack for temporary allocations.
Why it matters: These habits help limit garbage collection during gameplay. - Timing unavoidable garbage collection: Explain why and when the section recommends manually calling System.GC.Collect() if collection is unavoidable.
Why it matters: Choosing a less disruptive time for unavoidable collection can reduce its effect on the player. - Object reuse through pools: State how object pools are presented as another way to avoid triggering the Garbage Collector.
Why it matters: Reusing objects offers an alternative to repeatedly allocating replacements.
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.
- GameObjects alongside DOD logic: Explain why the Survivor game still needs Unity GameObjects for enemies even when its game logic is written using DOD.
Why it matters: It explains why GameObject spawning remains relevant to this DOD-based game.
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.
- Per-spawn GameObject allocation costs: Explain why instantiating an enemy GameObject when needed and destroying it when no longer needed is discouraged.
Why it matters: It explains the performance problem that object pools are introduced to address. - Delayed GameObject memory reuse: Describe what happens to a destroyed enemy GameObject’s memory before the garbage collector runs, including the effect of DestroyImmediate().
Why it matters: Destroying an enemy does not make its memory immediately available for another one.
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.
- Level-wide enemy pool lifecycle: Describe how the Survivor example creates a pool sized for a level’s enemies, starts its GameObjects inactive, and destroys them together when the level ends.
Why it matters: Allocating and destroying enemies at level boundaries keeps those memory operations out of ordinary spawning and removal. - Enemy reuse during gameplay: Trace how spawning takes an unused enemy GameObject from the pool, activates it, and later deactivates it for reuse; explain the memory-performance benefit.
Why it matters: Reusing enemies lets the game spawn and remove them without repeatedly creating garbage during gameplay.
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.
- Cost of resizing pools: Explain why resizing a full object pool is the wrong response and use the C# List example to illustrate the cost.
Why it matters: Avoiding growth when the pool is busiest prevents a costly allocation at the point of high memory use. - Enforced pool capacity limits: Describe how to choose a pool size using the level's maximum object count, rules that enforce it, and the minimum target device's capacity.
Why it matters: A capacity is reliable only when gameplay cannot exceed it and the supported hardware can handle it. - Full pool response: Describe what happens when a spawn would exceed the pool and how developers should address the event afterward.
Why it matters: A full pool should be handled without turning a spawn attempt into a costly runtime allocation.
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.
- Fixed Size Data Pools: Explain why DOD treats enemy data arrays as pools and allocates them alongside GameObject pools when loading a level.
Why it matters: Thinking of arrays as pools makes their capacity and allocation timing deliberate. - Active Flag Occupancy Bug: Trace why using a GameObject’s active boolean for both enemy occupancy and visibility breaks the pause-and-unpause behavior.
Why it matters: A visibility change should not silently change which pooled enemies the game considers in use. - Separate Alive Array Workflow: Describe how the Alive array and GameObject active flag are used when spawning, removing, pausing, and unpausing enemies.
Why it matters: Separate flags let the game hide enemies temporarily without losing track of available pool slots. - Logic And Visual Data Separation: Justify keeping the Alive array in EnemyData while leaving the GameObject pool outside it.
Why it matters: Keeping gameplay state independent of GameObjects makes it usable without a rendered scene.
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.
- Pooled gameplay data reset: Trace how a reused enemy can inherit stale gameplay data, and explain why every pooled slot must be initialized when it is reused.
Why it matters: A pooled enemy can appear to work on its first spawn but behave incorrectly after its slot is reused. - Pooled GameObject lifecycle pitfall: Explain why a script that shows a spawn effect in Start() can work the first time a pooled enemy appears but fail when that GameObject is reused.
Why it matters: Code attached to a pooled GameObject may not run at the point a developer expects when the object is reused.
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.
- Common and level allocation lifecycle: Trace when common and level-specific data are allocated and released, and explain how that arrangement avoids holes in common data.
Why it matters: Separating common and level data lets the game unload a level without fragmenting the data it still needs. - Stable menu memory baseline: Explain how repeated level loading and unloading makes memory use easier to check for leaks and capacity problems.
Why it matters: A repeatable memory baseline gives QA a practical way to spot leaks without playing through a level repeatedly. - Gameplay allocation avoidance benefits: Explain why allocating needed memory during loading and reusing it during gameplay helps avoid performance drops and makes out-of-memory failures easier to diagnose.
Why it matters: Avoiding allocation during play reduces unexpected slowdowns and the risk of a frustrating crash that costs the player progress.
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.
- Allocation timing and performance: Explain why when data is allocated and deallocated matters in DOD: poor timing can fragment memory or trigger garbage collection during play, hurting the user experience.
Why it matters: Avoiding poorly timed memory work helps keep gameplay from being disrupted. - Pooling and loading allocations: Describe the recommended allocation pattern: use object pools to avoid fragmentation and garbage-collector triggers, and ideally allocate a level’s needed memory during loading rather than allocate or deallocate heap memory while it runs.
Why it matters: Preparing memory before a level runs reduces the chance of memory-management work interrupting play.
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
- GameOver() (chapter 5) · The earlier discussion of garbage-collector-related slowdowns provides context for the expected collection after deallocation.
- Visualizing enemy changes on the board (chapter 6) · The performance cost associated with garbage collection.
- List and dynamic resizing (chapter 6) · The earlier discussion of memory growth and object pools.
- Summary (chapter 11) · Managing object lifetimes rather than repeatedly creating and discarding objects.
- AllocateGameData() - Allocating all the data for our game (chapter 4) · Allocating game data relies on the idea of preparing arrays for the game's capacity.
- Using the AliveEnemyIndices array (chapter 6) · The later alive-enemy indices array builds on tracking which enemy slots are in use.
- Spawning and positioning enemies (chapter 6) · Spawning enemies depends on identifying available enemy slots.
- Storing the save data (chapter 7) · Storing save data uses the distinction between logic state and recreatable visual data.
Concepts explained in this chapter
- object poolA preallocated collection of slots that holds data or objects for reuse.
- garbage collectionThe automatic process of identifying and reclaiming memory occupied by unreachable objects.
- memory fragmentationMemory fragmentation is the condition in which available memory is divided into separated holes that may not accommodate a requested contiguous allocation.
- memory allocationThe reservation of memory space for storing program data during execution.
- data-oriented programmingAn approach to structuring software around the flow and transformation of data rather than relationships between objects.
- logic dataData representing the state and rules of a game's simulation independently of its visual representation.
- visual dataVisual data represents the scene objects and presentation state used to display a game's simulation.
- heapA region of runtime memory used for dynamically allocated data whose lifetime is managed separately from function call frames.
- stack memoryA region of runtime memory typically used for function call frames and short-lived local values.
- arrayAn indexed sequence of elements, typically stored in contiguous memory.
- temporary dataRuntime data whose lifetime spans only a short operation or update rather than the entire gameplay session.
- contiguous memory layoutA region of memory in which elements occupy adjacent addresses without gaps between them.
- first-in-last-out structureAn ordering discipline in which the most recently added item is the first removed.
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.
