问题描述
我有一个 C 代码程序在我的计算机上运行,我需要检查我的程序在运行时使用了多少内存.有什么工具可以使用吗?
I have a C-code program running in my computer and I need to check how much memory my program uses when it is running. Is there any tool that I can use?
PS:在这种情况下,massif 工具 (valgrind
) 是否有效?我已经运行了它,但我不知道如何解释它创建的图形.
PS: Does massif tool (valgrind
) work in this case? I already ran it but I don't know how to interpret the graphic created by it.
PPS:我使用的是 Linux (eOs).
PPS: I'm using Linux (eOs).
推荐答案
Massif 是一个很棒的工具,可以显示您的程序随时间使用的内存量.不过,它也有一些缺点:
Massif is a great tool that displays how much memory your program uses over time. It has some drawbacks, though:
- 所有 Valgrind 工具都很慢;如果您的程序很慢,Massif 会进一步减慢它的速度(我认为是 10 倍).
- 您必须重新启动程序才能使用它,如果您的程序已经在运行,则无法使用 Valgrind(和 Massif)拦截它.
- 如果您希望 Massif 报告显示代码内存中的使用位置,则必须使用-g"标志编译您的程序.
如果你用你的程序运行 Massif,你可以运行
If you run Massif with your program, you can run
ms_print massif.out.1234 > output
您基本上不需要输出"部分,但我发现用更少的(或其他一些文本文件阅读器)解释结果比仅仅滚动终端历史记录更容易.在输出的顶部,您将看到内存消耗随时间变化的图表.在其下方,您将看到任意时间间隔的一些快照,您可以从中找到代码中占用最多内存的位置.
You don't essentially need the 'output'-part, but I find it easier to interpret the results with less (or some other text file reader) than just scrolling through the terminal history. At the top of output, you will see a graph of your memory consumption over time. Below that, you will see some snapshots from arbitrary time intervals from where you can locate the places in code which took most memory.
有一些图形工具可以解释 Massif 结果,但我从未觉得我需要它们.研究来自 ms_print 的报告,您将学会解释其结果.
There are some graphical tools to interpret Massif results, but I have never felt that I require them. Study the report from ms_print and you will learn to interpret its results.
祝你好运!
这篇关于检查我的程序使用了多少内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!