Glossary · Automation software engineering and architecture
Dynamic memory allocation
Also known as: Heap allocation
German: Dynamische Speicherzuweisung
In programming, dynamic memory allocation is the reservation of memory at runtime from a heap, in amounts determined while the program runs, with the memory later released explicitly or by garbage collection. It contrasts with static allocation, where memory is reserved when the program is built or started.
- Software engineering
In one sentence
Dynamic memory allocation reserves memory at runtime from a heap and releases it later, instead of reserving it at build time.
Example
The firmware coding rules forbid dynamic memory allocation after startup, so all message buffers are allocated once during initialization.
How it applies
- Engineering: Dynamic allocation is flexible but brings risks: allocation can fail, memory can fragment, allocation time may be unpredictable and errors cause leaks or Dangling pointer defects. For these reasons, real-time and safety-related software often avoids it after initialization.
- Standards: Coding standards for embedded and safety-related software, such as MISRA C, restrict dynamic memory use, and IEC 61508-3 lists its avoidance among recommended measures for higher integrity levels. Most IEC 61131-3 programs use static allocation by design.
- Documentation: Developer documentation should state the project's memory allocation rules and memory budgets. For long-running systems, document how memory use is monitored, since slow leaks may only show after weeks of operation.
Dynamic allocation vs. garbage collection
With manual dynamic allocation, the program frees memory explicitly. Garbage collection frees memory automatically when it is no longer referenced, avoiding many errors but introducing pauses that can affect timing.