liveBook · Manning

Memory allocator

As explained in Rust in Action: Systems programming concepts and techniques

An allocator is a program component that manages dynamic memory requests and may optimize interaction with the operating system.

A memory allocator is the intermediary between a running program and the operating system. It manages requests for dynamic memory, returns usable storage, and releases storage that is no longer needed. Allocators can optimize these operations by avoiding repeated work in the OS and CPU. section 6.3.3

Dynamic allocation follows three steps: request memory from the operating system, use it in the program, and release it. Heap data is flexible because pointers can change, but access is generally slower than stack access because heap values may be non-contiguous and require pointer dereferencing. section 6.3.3

Reporting allocations in Rust

A Rust program can install a custom global allocator by implementing GlobalAlloc and delegating to System. The allocator can measure each allocation with Instant, while a graphical particle program creates and destroys heap objects. Running it with cargo run -q 2> alloc.tsv records allocation data for inspection.

Allocation time is difficult to predict: measurements show no clear relationship between requested size and duration, even for repeated requests of the same size. This makes measurement important when allocation costs matter. section 6.3.4

Ways to reduce heap-allocation overhead include:

  • Reuse arrays of uninitialized objects instead of creating objects whenever they are needed, while recognizing that bypassing Rust lifetime checks can be dangerous. section 6.3.4
  • Choose an allocator tuned to the application's memory-access profile, because allocators often perform best for particular allocation sizes. section 6.3.4
  • Use arena or TypedArena allocation when suitable; allocation and freeing then occur when the arena is created and destroyed. section 6.3.4

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Defines the allocation workflow, demonstrates a reporting allocator, and measures timing, but stops short of allocator implementation internals.
Examples
Uses a Rust particle application, a custom GlobalAlloc wrapper, allocation logs, and a plotted allocation-time comparison.
What it assumes
Assumes basic program and memory concepts while explaining stack, heap, pointers, system calls, and allocation from fundamentals.
What you can do after
Diagnose allocation costs with a custom Rust global allocator and reduce overhead using reuse, tuned allocators, or arenas.
How it connects
Connects allocators to dynamic memory allocation, operating-system system calls, stack and heap storage, pointers, arenas, and Rust lifetime checks.

Also explained in

Compare how 2 books explain memory allocator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.