Skip to content

Memory Management

Madrajib Lab edited this page Dec 10, 2025 · 1 revision
  • Static Allocation (Preferred): This includes global variables, static variables, and local variables on the stack. The memory size is fixed at compile time.
    • Advantage: Extremely fast and deterministic (guaranteed timing).No risk of fragmentation or allocation failure at run time (unless you run out of stack space).
  • Dynamic Allocation ($\text{malloc}/\text{free}$): Allocates memory from the Heap at run-time.
    • Avoidance Reasons (as detailed previously):
      1. Non-Determinism: The time taken for $\text{malloc}$ to find a suitable block varies based on heap state, violating hard real-time deadlines.
      2. Fragmentation: Repeated allocations/deallocations lead to small, unusable holes in memory, causing large allocation requests to fail unpredictably.
      3. Failure Handling: In robust systems, an $\text{malloc}$ failure must be handled. In embedded systems, continuous operation is often critical, making predictable static allocation safer.
  • Alternative (Memory Pools): If dynamic-like allocation is needed, embedded systems use Memory Pools (or slabs). This pre-allocates a large contiguous block of memory and divides it into fixed-size chunks.
    • Advantage: Allocation and deallocation become deterministic (just marking a chunk as used/free) and eliminate fragmentation (since all chunks are the same size).

Clone this wiki locally