tcmalloc 的使用:https://goog-perftools.sourceforge.net/doc/tcmalloc.html
GWP-ASan原理:
1 | https://google.github.io/tcmalloc/gwp-asan.html#what-should-i-set-the-sampling-rate-to |
tcmalloc 的使用:https://goog-perftools.sourceforge.net/doc/tcmalloc.html
GWP-ASan原理:
1 | https://google.github.io/tcmalloc/gwp-asan.html#what-should-i-set-the-sampling-rate-to |
ptmalloc 来自于 glibc 2.27 版本,以 64 位操作系统为例。
先贴出 malloc_chunk
的定义
1 | struct malloc_chunk { |
其中 __libc_malloc
是 malloc 函数的原型。
malloc_chunk 的定义:
1 | struct malloc_chunk { |
已经分配的 chunk 格式如下:
jeprof 依赖 perl 环境,需要搭建 perl 环境。注意 perl 中有一些脚本使用的是绝对路径,需要放在绝对目录中
然后使用 jeprof 解析 heap 的时候,出现如下的问题:
1 | jeprof /opt/WM_B/APP/HAVP/linux-arm/ANP/bin/pavaro ./custom_prof.mem_profiler.110003.20230307_114334_114347.heap |
解决第一个问题,jeprof 使用了 file,并且是 /usr/bin/file 这个路径下的 file。注意 file 命令需要设置 magic file。
prof 配置的初始化路径
1 | malloc_init() 会被 constructor 调用 |
内存分配流程
1 | C 库内存管理函数 -> imalloc() -> imalloc_body() |
arena 是 jemalloc 最重要的部分,内存由一定数量的 arenas 负责管理。每个用户线程都会被绑定到一个 arena 上,线程采用 round-robin 轮询的方式选择可用的 arena 进行内存分配,为了减少线程之间的锁竞争,默认每个 CPU 会分配 4 个 arena。
bin 用于管理不同档位的内存单元,每个 bin 管理的内存大小是按分类依次递增。因为 jemalloc 中小内存的分配是基于 Slab 算法完成的,所以会产生不同类别的内存块。
chunk 是负责管理用户内存块的数据结构,chunk 以 Page 为单位管理内存,默认大小是 4M,即 1024 个连续的页。每个 chunk 可被用于多次小内存的申请,但是在大内存分配的场景下只能分配一次。
编译 jemalloc
1 | # 需要在编译时指定 `--enable-prof` 参数,`--enable-prof-libunwind` 打开 libunwind 堆栈。 |
问题:
1 | 一、 交叉编译后,在 arm 上使用出现 <jemalloc>: Unsupported system page size |