【MLA】一种内存泄漏分析方法

项目地址:skullboyer/MLA (github.com)

介绍

MLA 即 Memory Leak Analyzer,是一个排查内存泄漏的分析器

实现机制是在malloc时记录分配位置信息,在free时记录释放位置信息,通过两者计数作差可得是否存在泄漏

快速开始

你可以使用提供的脚本do.sh来快速使用本代码库

可以使用./do.sh help命令

-*- help -*-
usage: ./do.sh [generate] [make] [exec] [clean] [help]
[generate]: -g -G generate
Example usage of the MLA mechanism
$ ./do.sh -g MLA
$ ./do.sh make
Self-validation of MLA mechanisms
$ ./do.sh -g SV
$ ./do.sh make
LOG mechanism implementation analysis
$ ./do.sh -g LOG
$ ./do.sh make
Execute the program to view the results
$ ./do.sh exec
Remove unnecessary code
$ ./do.sh clean

如何使用

你只需两步就可以开始使用了

1、适配mla.h文件中的两个接口mallocfree

/* MLA内部使用的内存管理接口 */
#define MLA_MALLOC(size) malloc(size)
#define MLA_FREE(addr) free(addr)
/* 对外提供使用的内存泄漏检查的分配释放接口 */
#define PORT_MALLOC(size) MlaMalloc(size, __FILENAME__, __func__, __LINE__)
#define PORT_FREE(addr) MlaFree(addr, __FILENAME__, __func__, __LINE__)

2、在你的代码初始化部分加入接口MlaInit,在查看内存泄漏信息的地方调用接口MlaOutput即可

示例:

通过自证清白来演示MLA的用法

$ ./do.sh -g SV
Generate a example version of the MLA file.

执行上述命令后会生成一些文件,这些是MLA自证的测试文件

$ ./do.sh make
$ ./do.sh exec

执行上述命令后会输出MLA的分析信息,借助Diff字段可以清晰看出有没有内存泄漏

MLA Verbose部分可以看到详细的内存分配和释放信息,包括代码文件名、行数、函数以及分配大小、释放次数等信息

  • 不存在内存泄漏
-- SV_MlaOutput:
* *
********************************************** Memory Leak Analyzer **********************************************
* *
* M L A N O N E *
  • 泄露信息概览
-- MlaOutput:
* *
********************************************** Memory Leak Analyzer **********************************************
* *
Caller Hash Malloc Free Diff
sv_mla.c:316 SV_MlaMalloc 52f06d09 3 3 0
sv_mla.c:214 MlaMallocRecorder 1239e656 1 1 0
sv_mla.c:286 MlaFreeRecorder 49583dd0 2 2 0
  • 泄露信息细节
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% MLA Verbose %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*
>1
Caller Size Malloc Free Diff
sv_mla.c:316 SV_MlaMalloc 12 3 3 0
*----------------------------------------------------------------------------------------------------------------*
|Verbose: malloc free |
| 1. (12)B - [3] sv_mla.c:357 SV_MlaFree - [3] |
*----------------------------------------------------------------------------------------------------------------*
>2
Caller Size Malloc Free Diff
sv_mla.c:214 MlaMallocRecorder 104 1 1 0
*----------------------------------------------------------------------------------------------------------------*
|Verbose: malloc free |
| 1. (104)B - [1] sv_mla.c:201 MlaDelItem - [1] |
*----------------------------------------------------------------------------------------------------------------*
>3
Caller Size Malloc Free Diff
sv_mla.c:286 MlaFreeRecorder 88 2 2 0
*----------------------------------------------------------------------------------------------------------------*
|Verbose: malloc free |
| 1. (88)B - [2] sv_mla.c:153 MlaProcessFreeNode - [2] |
*----------------------------------------------------------------------------------------------------------------*

共同进步

欢迎大家使用并issue反馈

posted @   壹点灵异  阅读(295)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
历史上的今天:
2019-01-23 【rt-thread】ENV构建STM32-BSP报错
点击右上角即可分享
微信分享提示