-
Notifications
You must be signed in to change notification settings - Fork 0
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):
-
Non-Determinism: The time taken for
$\text{malloc}$ to find a suitable block varies based on heap state, violating hard real-time deadlines. - Fragmentation: Repeated allocations/deallocations lead to small, unusable holes in memory, causing large allocation requests to fail unpredictably.
-
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.
-
Non-Determinism: The time taken for
- Avoidance Reasons (as detailed previously):
-
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).