liveBook · Manning

Chapter 6: Memory

What you will learn in chapter 6 of Rust in Action: Systems programming concepts and techniques

Introduces memory as a shared resource and explains that the OS gives programs a simplified view of its availability and location.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces memory as a shared resource and explains that the OS gives programs a simplified view of its availability and location.

Key terms: memory, OS, shared resource, arbiter

6.1 Pointers

This section explains how pointers identify memory, distinguishes addresses, pointers, and references, and introduces the guarantees Rust references provide.

Key terms: pointer, memory address, usize, address space, raw pointer, reference, dynamically sized types, referent, aligned, length

6.2 Exploring Rust’s reference and pointer types

Introduces the book’s distinction between references and pointers, uses a small example to relate references to addresses in a shared address space, and notes a performance use for Cow.

Key terms: references, pointers, raw pointers, safety guarantees, addresses, address space, B, C, Cow, copy on write

6.2.1 Raw pointers in Rust

This section explains what Rust raw pointers represent, why dereferencing them is unsafe, and when their use may be justified.

Key terms: raw pointer, *const T, *mut T, memory address, dereferencing, unsafe block, referent, references, raw pointers, extra guarantees

6.2.2 Rust’s pointer ecosystem

This section introduces Rust’s range of pointer types and explains interior mutability and shared ownership as useful capabilities with runtime costs.

Key terms: raw pointers, smart pointer, wrapper type, fat pointer, thin pointer, usize, interior mutability, Cell<T>, RefCell<T>, Rc<T>

6.2.3 Smart pointer building blocks

The section identifies Rust’s extensible smart-pointer building blocks and briefly explains what several of them are used for.

Key terms: smart pointer, core::ptr::Unique, core::ptr::Shared, Box<T>, Rc<T>, Arc<T>, std::rc::Weak, std::arc::Weak, Rc, Arc

6.3 Providing programs with memory for their data

Introduces the stack–heap performance tradeoff and the compile-time size requirement behind the advice to prefer stack-friendly types in Rust.

Key terms: stack, heap, Sized, compile time

6.3.1 The stack

This section explains how stack frames support function calls and local variables, then shows how Rust functions can accept both string slices and owned strings.

Key terms: stack frame, stack pointer, Last In, First Out (LIFO), call site, stack, stack frames, local variables, &str, String, AsRef<str>

6.3.2 The heap

Introduces the heap as an area of program memory, explains why some values need it, and shows how Box<T> provides pointer-based access to heap values.

Key terms: heap, String, Vec<T>, dynamically sized types, slices, Box<T>, Box::new(T), pointer, stack, dereference operator

6.3.3 What is dynamic memory allocation?

The section explains how programs obtain and release memory dynamically, why stack and heap data differ in performance and flexibility, and how a toy program begins to measure allocation costs.

Key terms: dynamic memory allocation, allocator, system call, stack, heap, contiguous layout, cache-friendly, pointer, ReportingAllocator, dynamic memory allocations

6.3.4 Analyzing the impact of dynamic memory allocation

The section uses allocation timing data to show why heap allocation cost can be unpredictable, then considers when that cost matters and how to reduce allocations.

Key terms: heap allocation, allocation size, nanoseconds, allocation, delay, arrays of uninitialized objects, allocator, arena::Arena, arena::TypedArena, lifetime checks

6.4 Virtual memory

Introduces virtual memory by explaining why efficient memory access matters to CPU performance, without yet defining virtual memory.

Key terms: CPUs, memory, computer architecture

6.4.1 Background

Defines the basic terms needed to discuss a running program’s virtual memory and how its addresses relate to physical memory.

Key terms: virtual memory, real memory, address space, page table, MMU, page, word, segment, usize, isize

6.4.2 Step 1: Having a process scan its own memory

The section tests the idea that a process’s memory is one continuous range, using failed scans and variable addresses to show that some addresses are inaccessible while valid values occupy scattered pockets of virtual address space.

Key terms: NULL pointer, raw pointer, unsafe block, segmentation fault, virtual addresses, address space, stack, heap

6.4.3 Translating virtual addresses to physical addresses

This section explains how virtual addresses map to physical memory, what page-based mapping enables, and why memory layout affects performance.

Key terms: virtual addresses, physical addresses, memory management unit (MMU), pages, translation lookaside buffer, TLB, hot working portions, cache line, row-wise, column-wise

6.4.4 Step 2: Working with the OS to scan an address space

The section explains that a running program can inspect its address space by requesting information from the OS through the Windows API.

Key terms: address space, virtual address, physical address, system call, Windows API, KERNEL.DLL

6.4.5 Step 3: Reading from and writing to process memory

This section explains that operating systems let programs read and write process memory and names the corresponding Windows and Linux APIs.

Key terms: process memory, Just-In-Time compilers (JITs), process_vm_readv(), process_vm_writev(), ReadProcessMemory(), WriteProcessMemory()

6.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 5: Data in depth Chapter 7: Files and storage →

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.