绘制进程的内存使用情况图

发布于 2024-12-13 18:59:31 字数 1809 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

Saygoodbye 2024-12-20 18:59:31

我找不到任何真正的工具来做到这一点。

但我找到了一组简洁的脚本可以做到这一点。

使用这个小 bash 循环进行日志记录:

while true; do
ps -C <ProgramName> -o pid=,%mem=,vsz= >> /tmp/mem.log
gnuplot /tmp/show_mem.plt
sleep 1
done &

这将创建一个名为 /tmp/mem.log 的漂亮的小内存使用日志文件。然后,它使用以下脚本使用 gnuplot 生成数据图像(将其放入 /tmp/show_mem.plt):

set term png small size 800,600
set output "mem-graph.png"

set ylabel "VSZ"
set y2label "%MEM"

set ytics nomirror
set y2tics nomirror in

set yrange [0:*]
set y2range [0:*]

plot "/tmp/mem.log" using 3 with lines axes x1y1 title "VSZ", \
     "/tmp/mem.log" using 2 with lines axes x1y2 title "%MEM"

然后使用默认的 GNOME 图像查看器打开图像,当它打开时,它会不断重新加载图像变化。因此,如果上述所有循环都是后台的,那么您似乎在图像查看器中运行了一个令人惊叹的内存使用情况图形工具:)

我现在正在跟踪的进程如下所示:
内存使用量上升图表

看来我确实有一些内存问题:(

其中大部分内容是从 http://brunogirin.blogspot.com.au/2010/09/memory-usage-graphs-with-ps-and-gnuplot.html,应得的信用。

I couldn't find any real tools to do it.

But I have found a neat small set of scripts that'll do it.

Using this little bash loop to do the logging:

while true; do
ps -C <ProgramName> -o pid=,%mem=,vsz= >> /tmp/mem.log
gnuplot /tmp/show_mem.plt
sleep 1
done &

This will create a nice little log file of memory usage called /tmp/mem.log. Then it generates an image of the data with gnuplot using the following script (put this in /tmp/show_mem.plt):

set term png small size 800,600
set output "mem-graph.png"

set ylabel "VSZ"
set y2label "%MEM"

set ytics nomirror
set y2tics nomirror in

set yrange [0:*]
set y2range [0:*]

plot "/tmp/mem.log" using 3 with lines axes x1y1 title "VSZ", \
     "/tmp/mem.log" using 2 with lines axes x1y2 title "%MEM"

Then opening the image with the default GNOME image viewer it keeps reloading the image when it changes. So if all the above loop is backgrounded it will appear that you have an amazing memory usage graphing tool running within an image viewer :)

The process I'm tracking right now looks like this:
Graph of rising memory usage

It looks like I do have some memory issues :(

Much of this was ripped from http://brunogirin.blogspot.com.au/2010/09/memory-usage-graphs-with-ps-and-gnuplot.html, credit where it is due.

帅冕 2024-12-20 18:59:31

接受的答案对我有用,但我有点厌倦了每次想要测量内存时都进行大量复制/粘贴,因此我为此创建了一个小工具: com/parikls/mem_usage_ui" rel="noreferrer">https://github.com/parikls/mem_usage_ui

使用安装

pip install mem_usage_ui

在 shell 中键入 mem_usage_ui 启动浏览器 GUI。

结果如下:

在此处输入图像描述

The accepted answer worked for me, but I was a bit tired of doing so much copy/pasting every time I want to measure memory, so I've created a small tool for this: https://github.com/parikls/mem_usage_ui

Install using

pip install mem_usage_ui

Start the browser GUI by typing mem_usage_ui in the shell.

This is what the result will look like:

enter image description here

你是年少的欢喜 2024-12-20 18:59:31

目标的驻留集大小,$PID,进程可以流式传输到ttyplot用于实时显示:

while :; do grep -oP '^VmRSS:\s+\K\d+' /proc/$PID/status \
    | numfmt --from-unit Ki --to-unit Mi; sleep 1; done | ttyplot -u Mi

asciicast

上面的 ttyplot -u Mi 可以替换为 feedgnuplot --stream --line --point --xlen 20 以获得更多或 GUI 中的相同绘图(gnuplot,例如,具有图像导出功能)。 feedgnuplot 应该是 可使用 apt-get 安装。

Resident set size of the target, $PID, process can be streamed to ttyplot for live display:

while :; do grep -oP '^VmRSS:\s+\K\d+' /proc/$PID/status \
    | numfmt --from-unit Ki --to-unit Mi; sleep 1; done | ttyplot -u Mi

asciicast

ttyplot -u Mi above can be replaced with feedgnuplot --stream --line --point --xlen 20 to get more or less the same plot in GUI (of gnuplot, which, for instance, has image export). feedgnuplot should be installable with apt-get.

埋葬我深情 2024-12-20 18:59:31

Python 包 Memory Profiler 也适用于非 Python 可执行二进制文件。

使用 mprof 生成可执行文件的完整内存使用情况报告并绘制它:

mprof run <executable>
mprof plot

绘图将如下所示:

Performanceplot

它在 PyPI 上可用,因此可以安装:

pip install -U memory_profiler

Python package Memory Profiler works with non-Python executable binaries too.

Use mprof to generate a full memory usage report of your executable and to plot it:

mprof run <executable>
mprof plot

The plot would be something like this:

Performance plot

It is available on PyPI, so it can be installed:

pip install -U memory_profiler
送舟行 2024-12-20 18:59:31

我真的很喜欢使用“htop”而不是“top”。它非常丰富多彩,有很多选项,如设置、搜索、反转、树、排序、漂亮、删除。尝试一下:

$ sudo apt-get install htop

htop

I really like using "htop" instead of "top". It's very colorful and has a lot of options like setup, search, invert, tree, sort by, nice, kill. Give it a try:

$ sudo apt-get install htop

htop

醉殇 2024-12-20 18:59:31

顶部就能解决问题

top -b | grep {name of process}
top -b -p {PID}
top -b -u {userid}

Top will do the trick

top -b | grep {name of process}
top -b -p {PID}
top -b -u {userid}
温暖的光 2024-12-20 18:59:31

尝试在命令行中运行命令“top”。这将显示类似于 Windows 任务管理器的进程列表。

Try running the command "top" in the command line. This will display a list of processes similar to the windows task manager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文