Applicable Scenarios
- After the build completes, you need to understand the firmware's Flash/RAM usage.
- You need to find the functions or variables that occupy the most space.
- You need to compare the memory changes between two builds and track code bloat.
- You need to confirm whether the firmware is about to exceed the chip's memory limit.
- You need to parse the linker script to obtain the chip's total Flash/RAM capacity.
Required Inputs
.mapfile path (generated by the GCC/ARM linker), or ELF file path.- Optional linker script path (used to calculate the usage percentage).
- Optional warning threshold and Top-N symbol count.
Automatic Detection
--scanmode automatically searches the build directory for.mapand ELF files.- Parse the
.mapblock in theMemory Configurationfile to obtain the total FLASH/RAM capacity. - If a linker script is provided, parse the capacity information from the
MEMORY {}block. - Automatically detect the availability of
arm-none-eabi-sizeandarm-none-eabi-readelf.
Execution Steps
- First read references/usage.md, and confirm whether this is environment detection, build directory scanning, or performing analysis.
- If you are unsure whether the tool is available, first run the scripts/memory_analyzer.py mode of
--detectto confirm. - Use
--scan <build_dir>to automatically find files that can be analyzed. - Use
--map-fileor--elfto perform memory analysis. - Optionally use
--linker-scriptto obtain the total capacity and calculate the usage percentage. - Use
--top <N>to view the symbols that occupy the most space. - Use
--compareto compare the differences between two.mapfiles.
Failure Triage
- When
arm-none-eabi-sizeorarm-none-eabi-readelfis unavailable and ELF analysis is needed, returnenvironment-missing。 - When the specified
.mapor ELF file does not exist, returnartifact-missing。 - When the
.mapfile format cannot be recognized or parsing fails, returnproject-config-error。 - When the memory usage exceeds the warning threshold, include warning information in the successful result.
Platform Notes
.mapfile parsing uses pure regular expressions, requires no external tools, and is available on all platforms.- ELF analysis requires
arm-none-eabi-size, which is usually installed with the cross-compilation toolchain. - Linker script parsing uses pure regular expressions and supports the GCC LD format.
Output Conventions
- Output the size and address of each section (.text, .rodata, .data, .bss).
- Output Flash usage (.text + .rodata + .data) and RAM usage (.data + .bss).
- If total capacity information is available, output the usage percentage and warning status.
- Output the Top-N symbol list sorted by size.
- Comparison mode outputs the increases and decreases of each section and symbol.
Handoff Relationships
- When memory is about to overflow and optimization is needed, recommend that the user review the Top-N largest symbols.
- When you need to locate the position of a specific function in the code, you can use it together with the IDE or
grep.