1. mtrace
测试代码
#include <stdlib.h>
#include <stdio.h>
#include <mcheck.h>
#include <unistd.h>
#include "./t/t.h"
int main(int argc, char **argv)
{
mtrace();
setenv("MALLOC_TRACE", "/userdata/mem.txt", 1); // 设置保存记录文件位置
char *p = (char*)malloc(16);
free(p);
p = (char*)malloc(32);
char *p1 = (char*)malloc(32);
char *p2 = (char*)malloc(32);
// test();
while(1) {
sleep(1);
}
muntrace();
}
测试
gcc -g -no-pie -o test test.c // -no-pie 必须加,否则没行号
执行一次 ./test 会产生 test.log文件
mtrace ./test ./test.log
测速结果:
- top -p 进程号