liveBook · Manning

Heap

As explained in Latency

A heap is a dynamically sized memory region used for storage whose allocation changes during program execution.

The heap is a region of a process’s address space used for dynamic memory allocation. Its size changes at runtime as the application needs memory; the operating system can expand the region. Unlike a stack frame, which is allocated for a function call and removed when the function returns, heap storage is managed through allocation and reclamation. section 7.4.3 section 7.4.1

A dynamic allocator obtains larger chunks of memory and assigns smaller blocks to requests. In a simplified allocator, a freelist links unused blocks. An allocation searches for a block large enough to use, perhaps splitting it or continuing the search to avoid waste. A freed block can return to the list for a later request. Real allocators add layers and optimizations, so allocation is not necessarily a fixed-cost operation. section 7.4.1

Figure 7.3 The dynamic memory allocator manages a section of memory we call the heap. In this example, the dynamic memory allocator maintains a list of free blocks in memory (freelist), which is a linked list of all the available blocks.
Figure 7.2: The dynamic memory allocator manages a section of the memory we call the heap. In this example, the dynamic memory allocator maintains a list of free blocks in the memory (freelist), which is a linked list of all the available blocks.

The diagram shows allocated blocks beside a linked list of free blocks, making the allocator’s search and reuse of heap space concrete. section 7.4.1

Allocating a Rust value

A `Person` value containing a user ID and email can be dynamically allocated with `Box::new(Person { ... })`. Other interfaces for requesting dynamic storage include `new` in JavaScript, Java, and C++, and `malloc()` in C. section 7.4.1

With garbage collection, the application does not explicitly free each allocation: the runtime identifies unused heap blocks and reuses their memory. One approach allocates by advancing a pointer through a region, then collects when that region fills and moves surviving objects elsewhere. Collection can scan the heap and increase tail latency; incremental or parallel scanning can mitigate that cost. Heap management therefore affects both memory reuse and the timing of an application’s work. section 7.4.2

How Latency covers it

How deep it goes
Works through a simplified freelist allocator and a garbage-collected allocation scheme, while noting that production allocators have further layers.
Examples
Uses a Rust `Box` allocation, an allocator with 8- and 16-byte blocks, and diagrams of free blocks and address-space sections.
What it assumes
Introduces allocator mechanics with a simple freelist while using familiar allocations and function calls for context.
What you can do after
Evaluate/understand how heap allocation and garbage collection can introduce variable work and tail latency.
How it connects
Connects dynamic memory allocation to freelists, garbage collection, virtual address spaces, and the stack.

Also explained in

Compare how 7 books explain heap →

Related concepts

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