memory-analysis
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese固件内存分析
Firmware Memory Analysis
适用场景
Applicable Scenarios
- 构建完成后需要了解固件的 Flash/RAM 使用情况。
- 需要找出占用空间最大的函数或变量。
- 需要对比两次构建的内存变化,追踪代码膨胀。
- 需要确认固件是否即将超出芯片内存限制。
- 需要解析链接脚本获取芯片 Flash/RAM 总容量。
- Need to understand the Flash/RAM usage of the firmware after build completion.
- Need to find the functions or variables that occupy the most space.
- Need to compare memory changes between two builds and track code bloat.
- Need to confirm whether the firmware is about to exceed the chip memory limit.
- Need to parse linker scripts to obtain the total Flash/RAM capacity of the chip.
必要输入
Required Inputs
- 文件路径(GCC/ARM 链接器生成),或 ELF 文件路径。
.map - 可选的链接脚本路径(用于计算使用率百分比)。
- 可选的告警阈值和 Top-N 符号数量。
- Path to .map file (generated by GCC/ARM linker), or path to ELF file.
- Optional linker script path (used to calculate usage percentage).
- Optional alarm threshold and Top-N symbol count.
自动探测
Automatic Detection
- 模式自动搜索构建目录中的
--scan和 ELF 文件。.map - 解析 文件中的
.map块获取 FLASH/RAM 总容量。Memory Configuration - 若提供链接脚本,从 块解析容量信息。
MEMORY {} - 自动检测 和
arm-none-eabi-size可用性。arm-none-eabi-readelf
- mode automatically searches for .map and ELF files in the build directory.
--scan - Parse the block in the .map file to obtain the total FLASH/RAM capacity.
Memory Configuration - If a linker script is provided, parse capacity information from the block.
MEMORY {} - Automatically detect the availability of and
arm-none-eabi-size.arm-none-eabi-readelf
执行步骤
Execution Steps
- 先阅读 references/usage.md,确认本次是环境探测、构建目录扫描,还是执行分析。
- 若不确定工具是否可用,先运行 scripts/memory_analyzer.py 的 模式确认。
--detect - 使用 自动查找可分析的文件。
--scan <build_dir> - 使用 或
--map-file执行内存分析。--elf - 可选使用 获取总容量并计算使用率。
--linker-script - 使用 查看占用最大的符号。
--top <N> - 使用 对比两个
--compare文件的差异。.map
- First read references/usage.md to confirm whether this is environment detection, build directory scanning, or execution of analysis.
- If unsure whether the tool is available, first run the mode of scripts/memory_analyzer.py to confirm.
--detect - Use to automatically find analyzable files.
--scan <build_dir> - Use or
--map-fileto perform memory analysis.--elf - Optionally use to obtain total capacity and calculate usage rate.
--linker-script - Use to view the symbols that occupy the most space.
--top <N> - Use to compare differences between two .map files.
--compare
失败分流
Failure Diversion
- 当 或
arm-none-eabi-size不可用且需要 ELF 分析时,返回arm-none-eabi-readelf。environment-missing - 当指定的 或 ELF 文件不存在时,返回
.map。artifact-missing - 当 文件格式无法识别或解析失败时,返回
.map。project-config-error - 当内存使用率超过告警阈值时,在成功结果中附带告警信息。
- Return when
environment-missingorarm-none-eabi-sizeis unavailable and ELF analysis is required.arm-none-eabi-readelf - Return when the specified .map or ELF file does not exist.
artifact-missing - Return when the .map file format cannot be recognized or parsing fails.
project-config-error - When memory usage exceeds the alarm threshold, attach alarm information to the successful result.
平台说明
Platform Notes
- 文件解析使用纯正则,无需外部工具,全平台可用。
.map - ELF 分析需要 ,通常随交叉编译工具链安装。
arm-none-eabi-size - 链接脚本解析使用纯正则,支持 GCC LD 格式。
- .map file parsing uses pure regular expressions, no external tools required, available on all platforms.
- ELF analysis requires , which is usually installed with the cross-compilation toolchain.
arm-none-eabi-size - Linker script parsing uses pure regular expressions, supporting GCC LD format.
输出约定
Output Conventions
- 输出各 section(.text, .rodata, .data, .bss)的大小和地址。
- 输出 Flash 使用量(.text + .rodata + .data)和 RAM 使用量(.data + .bss)。
- 若有总容量信息,输出使用率百分比和告警状态。
- 输出按大小排序的 Top-N 符号列表。
- 对比模式输出各 section 和符号的增减变化。
- 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 usage percentage and alarm status.
- Output a sorted list of Top-N symbols by size.
- Comparison mode outputs the increase/decrease changes of each section and symbol.
交接关系
Handover Relationships
- 当内存即将溢出需要优化时,建议用户审查 Top-N 大符号。
- 当需要定位具体函数在代码中的位置时,可配合 IDE 或 使用。
grep
- When memory is about to overflow and optimization is needed, it is recommended that users review the Top-N large symbols.
- When needing to locate the specific position of a function in the code, it can be used with IDE or .
grep