性能测试工具gperftools使用-内存占用分析
2022/7/30 5:22:57
本文主要是介绍性能测试工具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)
这篇关于性能测试工具gperftools使用-内存占用分析的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行
- 2024-05-08阿里云域名注册流程,分享给第一次购买域名的新手站长!
- 2024-05-082024年,行业变动下的程序员应该首先学习哪种编程语言?