简体   繁体   English

监视程序在Linux中的内存使用情况

[英]monitor a program's memory usage in Linux

Are there any tools available in Linux which graphically or textually display memory usage for a program? Linux中是否有可用的工具以图形或文本方式显示程序的内存使用情况? For example, if I write a C++ program and would like to verify that objects are being allocated and deallocated properly in memory, are there applications available that would visually show the objects being instantiated and deleted? 例如,如果我编写一个C ++程序并想要验证对象是否正在内存中正确分配和解除分配,那么是否有可用的应用程序可以直观地显示正在实例化和删除的对象? When I used to program in Visual Studio, I remember stepping through a program and using a debug pane to monitor memory usage and am looking for something similar to that in Linux. 当我以前在Visual Studio中编程时,我记得单步执行程序并使用调试窗格来监视内存使用情况,并且正在寻找类似于Linux中的内容。

这不完全是你想要的,但看看Valgrind

while (/proc/<pid>/status)
 echo "VMSize: `ps -p <pid> -o vsize=`" >> ! mem.out
 pstack <pid> >> mem.out
 sleep 10
end

grep VMSize mem.out | awk -F':' '{print $2}' | sort -r -n | head -1 grep VMSize mem.out | awk -F':' '{print $2}' | sort -r -n | head -1 will give you peak memory. grep VMSize mem.out | awk -F':' '{print $2}' | sort -r -n | head -1会给你最高记忆。

Also use mem.out to see memory footprint and current stack correlation. 还可以使用mem.out查看内存占用和当前堆栈相关性。

我通常运行top以密切关注整体内存使用情况。

1) First run 1)首先运行

ps -u <your user id>

2) Get et pid of the process you want to monitor from output of 1) 2)从1)的输出中获取要监视的进程的et pid

3) Then run 3)然后跑

top -p <the pid of the process you want to monitor>

It is really difficult to work out how much memory a process is using on an operating system which supports virtual memory. 很难计算出一个进程在支持虚拟内存的操作系统上使用了多少内存。

The problem is not working out how much memory it's using, but how much of that is private and how much shared. 问题不在于它使用多少内存,而是多少内存是私有的以及共享多少内存。

You can look at /proc/pid/maps or /proc/pid/smaps (maybe). 你可以查看/ proc / pid / maps或/ proc / pid / smaps(也许)。 These files will only tell you how much memory the process has mapped into its address space, not how much it's using, and definitely not how much is shared with other processes in the system. 这些文件只会告诉您进程已映射到其地址空间的内存量,而不是它使用了多少,并且绝对不会与系统中的其他进程共享多少内存。

Even "private" maps can be shared because fork() does copy-on-write, so a private page could still be shared with some other (related - usually parent or sibling) process. 甚至可以共享“私有”映射,因为fork()执行copy-on-write,因此私有页面仍然可以与其他一些(相关的 - 通常是父级或兄弟级)进程共享。 Moreover, pages which have been mapped in but never used won't be consuming space at all. 此外,已映射但从未使用过的页面根本不会消耗空间。

The RSS (Resident set size) of each mapping can be seen, but that only tells you how much is resident (in RAM, as opposed to swapped out into a swap file, not yet allocated, or not yet demand-loaded from a mapped file), now how much is shared and with what. 可以看到每个映射的RSS(驻留集大小),但这只能告诉你驻留多少(在RAM中,而不是换成交换文件,尚未分配,或者还没有从映射中加载需求)文件),现在共享多少和什么。

I guess your best bet would be to count the amount of private anonymous memory, which might be ok, in some cases. 我想你最好的选择是计算私人匿名内存的数量,在某些情况下可能没问题。

conky ( screenshots ) is a great simple computer resource viewer that posts over the desktop wallpaper. conky( screenshots )是一个非常简单的计算机资源查看器,它通过桌面壁纸发布。 I keep an eye on memory use and specific program use. 我会密切关注内存使用和特定程序的使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM