Linux How to profile memory usage?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4690800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:28:42  来源:igfitidea点击:

How to profile memory usage?

c++linuxmemoryprofilervalgrind

提问by math

I am aware of Valgrind, but it just detects memory management issues. What I am searching is a tool that gives me an overview, which parts of my program do consume how much memory. A graphical representation with e.g. a tree map (as KCachegrind does for Callgrind) would be cool.

I am aware of Valgrind, but it just detects memory management issues. What I am searching is a tool that gives me an overview, which parts of my program do consume how much memory. A graphical representation with e.g. a tree map (as KCachegrind does for Callgrind) would be cool.

I am working on a Linux machine, so windows tools will not help me very much.

I am working on a Linux machine, so windows tools will not help me very much.

采纳答案by ismail

Use massif, which is part of the Valgrind tools. massif-visualizercan help you graph the data or you can just use the ms_printcommand.

Use massif, which is part of the Valgrind tools. massif-visualizercan help you graph the data or you can just use the ms_printcommand.

回答by Paolo M

Try out the heap profilerdelivered with gperftools, by Google. I've always built it from sources, but it's available as a precompiled packageunder several Linux distros.

Try out the heap profilerdelivered with gperftools, by Google. I've always built it from sources, but it's available as a precompiled packageunder several Linux distros.

It's as simple to use as linking a dynamic library to your executables and running the program. It collects information about every dynamic memory allocation (as far as I've seen) and save to disk a memory dumpevery time one of the following happens:

It's as simple to use as linking a dynamic library to your executables and running the program. It collects information about every dynamic memory allocation (as far as I've seen) and save to disk a memory dumpevery time one of the following happens:

  • HEAP_PROFILE_ALLOCATION_INTERVALbytes have been allocated by the program (default: 1Gb)
  • the high-water memory usage mark increases by HEAP_PROFILE_INUSE_INTERVALbytes (default: 100Mb)
  • HEAP_PROFILE_TIME_INTERVALseconds have elapsed (default: inactive)
  • You explicitly call HeapProfilerDump()from your code
  • HEAP_PROFILE_ALLOCATION_INTERVALbytes have been allocated by the program (default: 1Gb)
  • the high-water memory usage mark increases by HEAP_PROFILE_INUSE_INTERVALbytes (default: 100Mb)
  • HEAP_PROFILE_TIME_INTERVALseconds have elapsed (default: inactive)
  • You explicitly call HeapProfilerDump()from your code

The last one, in my experience, is the most useful because you can control exactly when to have a snapshot of the heap usageand then compare two different snapshots and see what's wrong.

The last one, in my experience, is the most useful because you can control exactly when to have a snapshot of the heap usageand then compare two different snapshots and see what's wrong.

Eventually, there are several possible output formats, like textual or graphical (in the form of a directed graph):

Eventually, there are several possible output formats, like textual or graphical (in the form of a directed graph):

Graph of memory usage

Graph of memory usage

Using this tool I've been able to spot incorrect memory usages that I couldn't find using Massif.

Using this tool I've been able to spot incorrect memory usages that I couldn't find using Massif.

回答by Matthieu Brucher

A "newer" option is HeapTrack. Contrary to massif, it is an instrumented version of malloc/freethat stores all the calls and dumps a log.

A "newer" option is HeapTrack. Contrary to massif, it is an instrumented version of malloc/freethat stores all the calls and dumps a log.

The GUI is nice (but requires Qt5 IIRC) and the results timings (because you may want to track time as well) are less biased than valgrind (as they are not emulated).

The GUI is nice (but requires Qt5 IIRC) and the results timings (because you may want to track time as well) are less biased than valgrind (as they are not emulated).

回答by Rohit

Use callgrind option with valgrind

Use callgrind option with valgrind