liveBook · Manning

Memory fragmentation

As explained in Embedded Software with Rust

The division of free memory into separated regions that can make allocation requests harder to satisfy.

Memory fragmentation divides free heap memory into separated regions, making allocation requests harder to satisfy and potentially causing allocation failure. section 7.2.5 section 7.0

In embedded firmware, fragmentation depends partly on how and how often the program allocates:

  • It is rare with infrequent or predictable allocations, but becomes a concern with unpredictable bursts over long uptimes. section 7.2.5
  • Repeated Vec growth can trigger reallocations that copy elements and free earlier blocks; Vec::with_capacity reserves space up front and reduces fragmentation. section 7.2.6
  • Use embedded-alloc with llff by default; consider tlsf or buddy_system_allocator only if profiling shows allocator behavior is a problem. section 7.2.5

Reserve for expected growth

For a Vec expected to hold 100 readings, Vec::with_capacity(100) makes one allocation up front instead of growing through repeated pushes. section 7.2.6

How Embedded Software with Rust covers it

How deep it goes
Connects allocation patterns to failure and reallocation, then offers mitigations without detailing allocator internals.
Examples
Contrasts Vec growth from zero to 100 elements with reserving capacity for 100 readings up front.
What you can do after
Apply Vec::with_capacity and profile allocator behavior before changing allocator implementations.
How it connects
Links heap allocation to allocators, Vec growth, heap sizing, and fixed-capacity containers.

Also explained in

Compare how 3 books explain memory fragmentation →

Related concepts

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