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.
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






