1. Concept and Classification of Memory
In computing systems, memory is used to store variables and intermediate data. The system memory can be divided into two types:
- Internal Storage Space (RAM): Typically refers to Random Access Memory, which allows fast data access and random access, but data is lost when power is off.
- External Storage Space: For example, hard drives or flash memory, which can retain data even when power is off.
In embedded systems, we mainly focus on the management of internal storage space (RAM), which is the core of memory management.
2. Design Philosophy of FreeRTOS Memory Management
The FreeRTOS operating system separates the kernel from memory management functions, enabling flexible memory management. The kernel only specifies the necessary memory management function prototypes without restricting the specific implementation methods. FreeRTOS provides multiple memory allocation algorithms (strategies), but they share a unified upper-level interface (API), allowing developers to choose the most suitable memory management strategy based on actual needs.
3. Dynamic Memory Allocation vs. Static Memory Allocation
- Static Memory Allocation: Memory allocation is determined at compile time, suitable for high-reliability systems, but with lower memory utilization efficiency.
- Dynamic Memory Allocation: Memory is allocated dynamically at runtime as needed, suitable for general business systems, with high memory utilization efficiency, but may lead to fragmentation issues.
Embedded systems typically choose memory allocation methods based on the characteristics of the application scenario.
4. Why Not Use C Standard Library Memory Management Functions?
Although functions like <span>malloc()</span>
and <span>free()</span>
are commonly used for dynamic memory allocation in general computing systems, using these functions in embedded real-time operating systems may lead to the following issues:
-
In small embedded systems, RAM capacity is limited, making <span>malloc()</span>
and<span>free()</span>
potentially unsuitable. -
The implementation of these functions typically occupies a large code space. -
Their execution time is unpredictable, which cannot guarantee real-time performance. -
They may lead to memory fragmentation. -
Linker configuration is complex and may overwrite the memory of other variables, increasing debugging difficulty.
Therefore, FreeRTOS provides a dedicated memory management mechanism to meet the requirements of embedded real-time systems for determinism and efficiency.
5. Memory Management Strategies in FreeRTOS
The FreeRTOS memory management module is responsible for managing system memory resources, including memory initialization, allocation, and release. To meet the needs of different embedded systems, FreeRTOS offers various memory allocation algorithms, each suitable for specific application scenarios.
FreeRTOS V9.0.0 provides five memory management algorithms:
- heap_1.c: The simplest memory allocation strategy, does not support memory release, suitable for systems with fixed memory requirements.
- heap_2.c: Allows memory release but may produce fragmentation.
- heap_3.c: Encapsulates standard
<span>malloc()</span>
and<span>free()</span>
, suitable for systems that need to use the standard library. - heap_4.c: A more complex memory allocation strategy that supports merging adjacent free memory blocks to reduce fragmentation.
- heap_5.c: Supports allocation from multiple memory regions, suitable for systems that require flexible memory management.
The source files for these memory management algorithms are located in <span>FreeRTOS\Source\portable\MemMang</span>
directory, and developers can choose the appropriate algorithm based on their needs.
6. Conclusion
The FreeRTOS memory management module maximizes memory utilization and reduces memory fragmentation through efficient memory allocation and release mechanisms. Developers can choose the appropriate memory management strategy based on application requirements to ensure that embedded systems operate stably and efficiently.