ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2022-07-29 12:36:00  阅读:261  来源: 互联网

标签:profile use MB profiler test 内存 heap 测试工具 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)

标签:profile,use,MB,profiler,test,内存,heap,测试工具,gperftools
来源: https://www.cnblogs.com/gnivor/p/16531828.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有