Memory allocation
Memory allocation is the process of reserving or assigning computer memory to hold program data or objects.
100 C++ Mistakes and How to Avoid Them
The process of reserving storage for program objects or data during program execution.
- How it is used
- Addresses dynamic storage ownership and cleanup in C++ resource-managing code.
- How deep it goes
- Works through raw allocation failures, ownership transfer, copy behavior, and exception cleanup using corrected code.
- Examples
- Uses a Buffer with duplicate owners, a file-reading constructor, and dynamically allocated test scores.
- What you can do after
- Apply std::unique_ptr and RAII to manage allocated resources and recognize common ownership and cleanup mistakes.
Read how 100 C++ Mistakes and How to Avoid Them explains memory allocation →
Refactoring to Rust
Memory allocation is the process of reserving and managing regions of computer memory for program data.
- How it is used
- Connects memory-management basics to exchanging data between a WebAssembly module and its host.
- How deep it goes
- Explains allocation and deallocation, then demonstrates an exported allocator and outlines the host’s memory-access sequence.
- Examples
- Uses a visitor’s name in a welcome message and a Rust vector-backed WebAssembly allocator as examples.
- What you can do after
- Build from scratch a basic WebAssembly allocation function in Rust and trace the host’s memory-access sequence.
Latency
Dynamic memory allocation is reserving and releasing storage during program execution from a runtime-managed memory region.
- How it is used
- Targets dynamic allocation as a potential source of latency and work to eliminate in performance-sensitive code.
- How deep it goes
- Explains a simplified free-list allocator through block selection, splitting, and reuse, while noting that real allocators add layers and optimizations.
- Examples
- Uses Rust Box allocation for a Person and compares recursive, memoized, and iterative Fibonacci implementations.
- What you can do after
- Apply benchmarking via Criterion to compare implementations and assess the effect of eliminating work, including dynamic allocation.
100 Go Mistakes and How to Avoid Them
Memory allocation is the process of reserving memory for data or runtime objects.
- How it is used
- Uses allocation behavior to identify and reduce performance costs in Go applications.
- How deep it goes
- Explains stack and heap behavior, escape analysis, benchmark results, compiler diagnostics, and several allocation-reduction approaches.
- Examples
- Compares value-returning and pointer-returning sum functions with inlining disabled and reports benchmark allocation counts.
- What you can do after
- Diagnose allocation behavior using Go benchmarks and compiler escape reports, then evaluate possible reductions.
Read how 100 Go Mistakes and How to Avoid Them explains memory allocation →
Code Like a Pro in Rust
Memory allocation is the process of reserving memory regions for data during program execution.
- How it is used
- Uses allocation choices to manage Rust data structures and support memory-constrained or specialized applications.
- How deep it goes
- Explains heap and stack behavior, Vec allocation, and custom allocator methods, including default resizing and layout requirements.
- Examples
- Shows heap-allocated Box, Vec, and String values, plus a pass-through allocator used with Vec.
- What you can do after
- Build a custom allocator and use it with Vec, or choose fixed-size structures when dynamic allocation is unnecessary.
Read how Code Like a Pro in Rust explains memory allocation →
Modern C
Dynamic allocation is obtaining and releasing object storage during program execution rather than solely at translation or function-entry time.
- How it is used
- Handles growing collections and irregular data by reclaiming storage during program execution.
- How deep it goes
- Explains allocation functions, sizing, void pointers, lifetime tracking, and invalid release cases, but defers complete initialization rules.
- Examples
- Uses a large vector of doubles, sizeof-based sizing, allocation-release pairs, and realloc as concrete illustrations.
- What you can do after
- Operate/configure storage with malloc, calloc, realloc, aligned_alloc, and free while avoiding leaks and invalid releases.
Rust in Action: Systems programming concepts and techniques
Memory allocation is the process of reserving memory for data or program execution.
- How it is used
- Motivates measuring heap allocation overhead and avoiding allocation where an operating system cannot provide it.
- How deep it goes
- Explains the allocator’s role and stack–heap trade-offs, then measures allocation timings and suggests strategies without implementing an arena.
- Examples
- Uses a graphical particle program, an allocation-timing plot, and a kernel that avoids dynamic allocation.
- What you can do after
- Diagnose allocation overhead by recording request sizes and durations and plotting the results with gnuplot.
Read how Rust in Action: Systems programming concepts and techniques explains memory allocation →
Go by Example
A memory allocation is a reservation of heap memory made during program execution, commonly counted per operation in performance measurements.
- How it is used
- Measures and reduces heap work while profiling Go code paths such as URL generation, argument parsing, result handling, and response reading.
- How deep it goes
- Explains profiling, benchmark comparison, escape-analysis output, and practical allocation trade-offs without detailing garbage-collector internals.
- Examples
- Uses URL string construction, a config flag parser, Result values in concurrent processing, and io.ReadAll on HTTP response bodies.
- What you can do after
- Diagnose/debug with pprof, benchmark memory metrics, benchstat, and compiler escape-analysis output to target unnecessary allocations.
Fast Python: High performance techniques for large datasets
- How it is used
- Uses memory-footprint analysis to reduce data representation costs and make room for parallel processes or in-memory algorithms.
- How deep it goes
- Explains container and object overhead and builds an iterative estimator, but cannot reliably account for every external-library allocation.
- Examples
- Compares NOAA file bytes with a list of integer objects and demonstrates estimating container and object sizes.
- What you can do after
- Evaluate Python object footprints with an iterative estimator while recognizing the limits of size estimates.
Read how Fast Python: High performance techniques for large datasets explains memory allocation →
Modern C, Third Edition
Dynamic allocation is a runtime mechanism that obtains storage for objects independently of ordinary declarations and releases it according to allocation operations.
- How it is used
- Supports growing collections, variable user input, large matrices, interaction graphs, and other data whose size changes at runtime.
- How deep it goes
- Explains storage creation, failure handling, specialized allocation functions, initialization, and release, while deferring fuller lifetime rules.
- Examples
- Uses a runtime-sized double vector, explicit element initialization, malloc failure checks, and eventual free.
- What you can do after
- Operate malloc-family functions to obtain, initialize, resize, and release runtime-sized storage while checking failure.
Read how Modern C, Third Edition explains memory allocation →
High Performance Unity Game Development
The reservation of memory space for storing program data during execution.
- How it is used
- Plans game data lifecycles to reduce memory fragmentation, garbage-collector slowdowns, and out-of-memory crashes.
- 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.
Read how High Performance Unity Game Development explains memory allocation →
PostgreSQL Mistakes and How to Avoid Them
Memory allocation is the assignment of available memory resources to database processes and operations.
- How it is used
- Uses PostgreSQL workload testing to expose unsafe per-query memory settings under high concurrency.
- How deep it goes
- Explains shared_buffers, per-plan-node work_mem multiplication, concurrency, parallel execution, and OOM recovery consequences.
- Examples
- Uses 100-client and 1000-client pgbench runs, configuration listings, workload output, and Linux OOM logs.
- What you can do after
- Diagnose memory pressure with pgbench and system logs, then avoid allocating excessive work_mem.
Read how PostgreSQL Mistakes and How to Avoid Them explains memory allocation →
Python Workout: 50 ten-minute exercises
Memory allocation is the process of reserving storage for data during program execution.
- How it is used
- Frames memory allocation as background knowledge for understanding list growth and investigating optimization problems.
- How deep it goes
- Explains spare list capacity and inspection with sys.getsizeof, but stops short of allocator internals or garbage-collector control.
- Examples
- Demonstrates repeated list appends, sys.getsizeof measurements, and output showing stepwise storage increases.
- What you can do after
- Evaluate list storage growth with sys.getsizeof and recognize when allocation details matter for optimization.
Read how Python Workout: 50 ten-minute exercises explains memory allocation →
Also covered in
- Troubleshooting Java: Read, debug, and optimize JVM applications
- CUDA for LLMs
- Writing for Developers: Blogs that get read
- JavaScript in DepthIn early access (MEAP)
- Troubleshooting Java, Second Edition
- Grokking GPU ProgrammingIn early access (MEAP)
What to know first
- garbage collectionGarbage collection is an automated process that identifies and reclaims or removes memory, data, or other items no longer reachable or needed by a system.
- referenceA reference is an indirect value, binding, or relationship that identifies or provides access to another object, value, resource, or source.
- C++C++ is a statically typed, general-purpose programming language that supports procedural, object-oriented, generic, and low-level systems programming.
- heapA heap is a runtime memory region from which dynamically allocated data is obtained and whose allocation and lifetime are managed separately from function call frames.
Related concepts
- mallocuses · malloc is a C standard-library function that allocates a specified number of bytes of uninitialized storage and returns a pointer to it or a null pointer if allocation fails.
- call stackcontrasts with · A call stack is a runtime LIFO structure that tracks active function or method calls, including their execution contexts and return information.
- memory allocatorpart of · A memory allocator is a software component or abstraction that manages the allocation and release of memory for program use.
- Boxused by · Box is a Rust owning smart pointer that provides indirection by storing a uniquely owned value in heap-allocated memory.
- heapused by · A heap is a runtime memory region from which dynamically allocated data is obtained and whose allocation and lifetime are managed separately from function call frames.
- performance issueproduces · A problem in an application’s use of resources or delivery of service, including but not limited to slow responses.
Questions about memory allocation
What is memory allocation?
Memory allocation is the process of reserving or assigning computer memory to hold program data or objects.
Which Manning books explain memory allocation?
100 C++ Mistakes and How to Avoid Them, Refactoring to Rust, Latency, 100 Go Mistakes and How to Avoid Them, Code Like a Pro in Rust, Modern C, Rust in Action: Systems programming concepts and techniques, Go by Example and 5 more.
What should I know before learning memory allocation?
Manning books that teach memory allocation usually introduce garbage collection, reference, C++ and heap first.
What is the difference between memory allocation and call stack?
Memory allocation is the process of reserving or assigning computer memory to hold program data or objects. A call stack is a runtime LIFO structure that tracks active function or method calls, including their execution contexts and return information.
What is memory allocation part of?
Memory allocation is part of memory allocator. A memory allocator is a software component or abstraction that manages the allocation and release of memory for program use.
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.












