部分文章内容为公开资料查询整理,原文出处可能未标注,如有侵权,请联系我,谢谢。邮箱地址:gnivor@163.com ►►►需要气球么?请点击我吧!

性能测试工具gperftools使用-内存占用分析

参考资料

性能测试工具gperftools使用-cpu分析 https://www.cnblogs.com/gnivor/p/11719958.html
gperftools使用 https://zhuanlan.zhihu.com/p/352260464
google heap profiler内存管理工具小试 https://blog.csdn.net/cica0cica/article/details/76919381

安装相关依赖

sudo yum install -y gperftools
sudo yum install -y gperftools-devel
sudo yum install pprof

测试代码

使用bazel构建编译(其他方式也可以)
目录结构:

.
├── test
│   ├── BUILD
│   ├── heap_profiler.h
│   └── test_heap_profiler.cpp
└── WORKSPACE

heap_profiler.h

#pragma once                                                                                                                                                           
extern "C" {
int HeapProfilerStart(const char* fname);
void HeapProfilerDump(const char* reason);
void HeapProfilerStop();
}

test_heap_profiler.cpp

#include <iostream>
#include "heap_profiler.h"

const int32_t k1024KB = 1024 * 1024;

// 分配100MB内存
void test1() {
  int i = 100;
  while (i--) {
    char *buf = new char[k1024KB];
  }
}

// 分配100MB内存
void test2() {
  int i = 100;
  while (i--) {
    char *buf = new char[k1024KB];
  }
}

int main() {
  HeapProfilerStart("./test.log");
  int i = 100;
  while (i--) {
    char *buf = new char[k1024KB];
  }
  test1();
  test2();
  test2();
  HeapProfilerStop();
  return 0;
}

编译

 bazel build -c opt --linkopt="/usr/lib64/libtcmalloc_and_profiler.so.4"  ... --compilation_mode=dbg

执行

每50MB生成1个profiling文件

 HEAP_PROFILE_ALLOCATION_INTERVAL=52428800 ./bazel-bin/test/test_heap_profiler

执行日志

Starting tracking the heap
Dumping heap profile to ./test.log.0001.heap (50 MB allocated cumulatively, 50 MB currently in use)
Dumping heap profile to ./test.log.0002.heap (100 MB allocated cumulatively, 100 MB currently in use)
Dumping heap profile to ./test.log.0003.heap (150 MB allocated cumulatively, 150 MB currently in use)
Dumping heap profile to ./test.log.0004.heap (200 MB allocated cumulatively, 200 MB currently in use)
Dumping heap profile to ./test.log.0005.heap (250 MB allocated cumulatively, 250 MB currently in use)
Dumping heap profile to ./test.log.0006.heap (300 MB allocated cumulatively, 300 MB currently in use)
Dumping heap profile to ./test.log.0007.heap (350 MB allocated cumulatively, 350 MB currently in use)
Dumping heap profile to ./test.log.0008.heap (400 MB allocated cumulatively, 400 MB currently in use)

可见每50MB dump了1个profile, 这个大小使用HEAP_PROFILE_ALLOCATION_INTERVAL控制

完整参数说明 https://gperftools.github.io/gperftools/heapprofile.html

参数名 默认值 解释
HEAP_PROFILE_ALLOCATION_INTERVAL default: 1073741824 (1 Gb) Dump heap profiling information each time the specified number of bytes has been allocated by the program.
HEAP_PROFILE_INUSE_INTERVAL default: 104857600 (100 Mb) Dump heap profiling information whenever the high-water memory usage mark increases by the specified number of bytes.
HEAP_PROFILE_TIME_INTERVAL default: 0 Dump heap profiling information each time the specified number of seconds has elapsed.
HEAPPROFILESIGNAL default: disabled Dump heap profiling information whenever the specified signal is sent to the process.
HEAP_PROFILE_MMAP default: false Profile mmap, mremap and sbrk calls in addition to malloc, calloc, realloc, and new. NOTE: this causes the profiler to profile calls internal to tcmalloc, since tcmalloc and friends use mmap and sbrk internally for allocations. One partial solution is to filter these allocations out when running pprof, with something like pprof --ignore='DoAllocWithArena
HEAP_PROFILE_ONLY_MMAP default: false Only profile mmap, mremap, and sbrk calls; do not profile malloc, calloc, realloc, or new.
HEAP_PROFILE_MMAP_LOG default: false Log mmap/munmap calls.

内存占用分析

 pprof --text bazel-bin/test/test_heap_profiler test.log.0008.heap 

结果

Using local file bazel-bin/test/test_heap_profiler.
Using local file test.log.0008.heap.
Total: 400.0 MB
   200.0  50.0%  50.0%    200.0  50.0% test2
   100.0  25.0%  75.0%    400.0 100.0% main
   100.0  25.0% 100.0%    100.0  25.0% test1
     0.0   0.0% 100.0%    400.0 100.0% __libc_start_main
     0.0   0.0% 100.0%    400.0 100.0% _start

每一列的解释

The first column contains the direct memory use in MB.
The fourth column contains memory use by the procedure and all of its callees.
The second and fifth columns are just percentage representations of the numbers in the first and fourth columns.
The third column is a cumulative sum of the second column (i.e., the kth entry in the third column is the sum of the first k entries in the second column.)

可见test2函数分配了200MB内存, test1分配了100MB内存, main总共分配400MB内存(自身100MB+test1 100MB + test2 200MB)

posted @ 2022-07-29 11:58  流了个火  阅读(3048)  评论(0编辑  收藏  举报
►►►需要气球么?请点击我吧!►►►
View My Stats