liveBook · Manning

Memory allocation

As explained in High Performance Unity Game Development

The reservation of memory space for storing program data during execution.

Memory allocation reserves space for program data during execution. In games, when and where memory is allocated and deallocated matters: poor timing can cause fragmentation, garbage-collector slowdowns, or out-of-memory crashes. section 3.5 section 3.0

A level-based allocation plan keeps memory use predictable:

  • Allocate common data, such as menus and shared object pools, before level-specific data. section 3.4
  • Allocate everything a level needs when it loads, then avoid heap allocations while it runs. section 3.4
  • When leaving the level, deallocate its data while keeping common data available. Repeating this cycle should not increase main-menu memory use. section 3.4 section 3.5
  • Group related allocations, deallocate in reverse order, use the stack for temporary allocations, and recycle objects with pools where possible. section 3.2.4 section 3.5

Survival-game level

Allocate menus and shared objects for the main menu, then allocate a level’s enemies and terrain as it loads. Unload that level data on exit, leaving the common allocation intact. section 3.4

Figure 3.13 An ideal memory allocation, with common data allocated first, then level-specific data.

Visualizes common data allocated before level-specific data. section 3.4

Allocate game data in one place rather than recreating it every time save or load logic runs. Reusing the existing data avoids repeated allocation and makes memory issues easier to locate. If collection is unavoidable, schedule it for a less disruptive moment, such as a loading screen. section 7.1.2 section 3.2.4

How High Performance Unity Game Development covers it

How deep it goes
Explains allocation order and level lifecycles, but does not detail allocator internals.
Examples
Uses a survival-game menu and level lifecycle, plus reused game data in save and load functions.
What you can do after
Apply via object pools and level-loading boundaries to limit heap allocation while gameplay runs.
How it connects
Connects memory allocation to object pools, garbage collection, memory fragmentation, and data-oriented design.

Also explained in

Compare how 13 books explain memory allocation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of High Performance Unity Game Development. The section links point to the parts of the book it draws on.