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












