吳彥廷
First, in the Posix demo simulation environment, switch between heap_3, heap_4, and heap_5, and write test code to simulate memory allocation behaviors with varying sizes and frequencies. Then, compare the memory allocation speed, fragmentation levels, and resource overhead of heap_3, heap_4, and heap_5 to identify the strengths, limitations, and appropriate use cases for each allocator. Additionally, integrate the mallocvis tool to observe the dynamic behavior of memory allocation and deallocation. Finally, propose improvement suggestions for heap_5 and implement the optimized solution.
Feature | heap_3 | heap_4 | heap_5 |
---|---|---|---|
Allocation Strategy | Uses C standard library malloc() and free() |
First-Fit algorithm | Multi-region First-Fit algorithm |
Multi-Memory Region Support | Not supported | Not supported | Supported |
Fragmentation Control | Cannot control | Uses best-fit strategy to reduce internal fragmentation | Provides flexibility to reduce fragmentation |
Memory Efficiency | Efficiency depends on the underlying C standard library | Highly efficient for small embedded systems | Supports dynamic memory regions, more effective for large embedded systems |
Overhead | Minimal (determined by the C standard library) | Moderate (additional metadata for the built-in allocator) | Moderate (additional data structures for region management) |
Configuration Complexity | Simple (just link the standard library) | Relatively simple (requires memory block size setup) | Relatively complex (requires configuration of multiple memory regions) |
Debugging and Tracking Support | Relies on the implementation of the C standard library | Provides detailed memory management data | Provides multi-region memory management data |
Use Case | Small systems or scenarios requiring integration with the standard library | General use cases, suitable for most embedded applications | Highly flexible, suitable for scenarios requiring multi-region memory allocation |
On an Ubuntu system, execute the following commands to install the necessary tools and libraries:
Update packages and install basic tools :
Install RISC-V toolchain :
Install mallocvis (for visualization tools)
Clone the source code from the official FreeRTOS repository :
Select the Posix_GCC demo project for testing :
Modify the FreeRTOSConfig.h file to select the heap management scheme :
Use the following command to compile the project:
In heap_3.c, add the following code to log allocation and deallocation behaviors :
Idea was as follows : I planned to modify the source code of heap3, heap4, and heap5 to include custom malloc and free functions, then test these using the Posix_gcc demo. Additionally, I intended to use mallocvis to observe memory management behavior in these heaps, particularly focusing on fragmentation analysis. However, I’ve encountered some issues that I’m currently trying to resolve:
Mallocvis cannot generate the expected complete visualization
My expectation was to see a clear graphical representation of memory allocation and deallocation behavior, but the actual output is incomplete and does not meet the intended requirements.
The allocated memory block sizes do not match the original settings
Initially, I configured the memory allocation to randomly select sizes from {32, 64, 128, 256, 512}. However, during execution, the allocated sizes were inconsistent, showing results like:
Allocations such as 82, 90, and 111 bytes deviate from the predefined sizes.
These problems are preventing me from completing the tests and observations as planned. I am currently working on troubleshooting and resolving these issues.
The modified pvPortMalloc function implements a Best-Fit Allocation strategy to reduce memory fragmentation by selecting the smallest free block that is large enough to fulfill the allocation request.
Key Changes for Best-Fit Allocation:
Both are initialized to NULL and SIZE_MAX respectively.
The modified vPortFree function incorporates Block Coalescing to merge adjacent free blocks when a block is released. This reduces fragmentation and makes larger contiguous memory blocks available for future allocations.
Key Changes for Block Coalescing: