[英]How can I track memory allocation of C++ standard library calls?
Consider this simple example: 考虑这个简单的例子:
#include <algorithm>
#include <iostream>
#include <list>
#include <numeric>
#include <random>
#include <vector>
#include <iterator>
int main()
{
std::list<int> l(10);
std::iota(l.begin(),l.end(),77);
std::vector<std::list<int>::iterator> v(l.size());
std::iota(v.begin(), v.end(), l.begin());
std::vector<int> dest;
std::copy_if(l.begin(), l.end(), std::back_inserter(dest), [](int i){return i%2==1;});
for(auto n : dest)
std::cout << n << " ";
return 0;
}
When run under Valgrind, it gives me the following output: 在Valgrind下运行时,它给出了以下输出:
==27353== total heap usage: 15 allocs, 15 frees, 380 bytes allocated
Is it possible to track exactly where those allocs occured (ie which data structure performed allocation and when exactly)? 是否有可能准确跟踪这些分配发生的位置(即哪个数据结构执行分配以及何时执行)?
The correct way to track C++ library allocation calls is to use Massif , a heap profiler tool (part of Valgrind): 跟踪C ++库分配调用的正确方法是使用Massif ,一个堆分析器工具(Valgrind的一部分):
Run Valgrind with Massif enabled: 运行Valgrind并启用Massif:
valgrind --tool=massif
Use ms_print
to analyze output from Massif: 使用
ms_print
分析Massif的输出:
ms_print massif.out.12345 (number varies)
Try giving valgrind the --keep-stacktraces=alloc
option. 尝试给valgrind
--keep-stacktraces=alloc
选项。 Note using that option will increase the overhead of using valgrind. 注意使用该选项会增加使用valgrind的开销。 Here are the docs http://valgrind.org/docs/manual/mc-manual.html#mc-manual.options so you can fine tune what valgrind captures.
以下是文档http://valgrind.org/docs/manual/mc-manual.html#mc-manual.options,因此您可以微调valgrind捕获的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.