如何在linux下监控php的内存使用情况?

发布于 2024-07-25 22:13:19 字数 237 浏览 7 评论 0原文

我过去曾使用 valgrinds Massif 工具来监控内存使用情况。

有谁知道如何捕获在 linux lighttpd 服务器上生成的 php 进程的内存使用情况?

我发现 Valgrind 无法附加到预运行进程(而且我事先也不知道 php 进程的 PID),

我只看到 lighttpd 的内存使用情况,而不是由 lighttpd cgi 模块生成的 PHP 进程。

提前致谢。

I have used valgrinds massif tool to monitor memory usage in the past.

Does anyone know how to capture memory use of php processes that are spawned on a linux lighttpd server?

I have found that Valgrind can not attach to a prerunning process (and I would not know the PID of the php process before hand anyway)

I only see lighttpd's memory usage, not the PHP processes that are spawned by lighttpd cgi module.

Thanks in advance.

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

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

发布评论

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

评论(4

橘虞初梦 2024-08-01 22:13:22

你不能使用“ps”工具吗?

$ ps -F -C php-cgi

UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
http     10794 10786  0  4073   228   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10795 10794  0  4073    28   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10796 10786  0  4073   228   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10797 10796  0  4613  3544   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
...

RSS 是进程的实际内存(驻留集)大小(以千字节为单位)。

在 bash 中总结一下(抱歉有点生疏)

#!/bin/bash

total=0
for i in `ps -C php-cgi -o rss=`
do
    total=$(($total + $i))
done
echo "Memory usage: $total kb"

# Output: Memory usage: 4540 kb

一句:

total=0; for i in `ps -C php-cgi -o rss=`; do total=$(($total+$i)); done; echo "Memory usage: $total kb";

我知道<中内存部分的可靠性em>ps 受到质疑,但至少它让您了解其用法。

Can't you use the 'ps' tool?

$ ps -F -C php-cgi

UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
http     10794 10786  0  4073   228   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10795 10794  0  4073    28   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10796 10786  0  4073   228   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
http     10797 10796  0  4613  3544   0 Jun09 ?        00:00:00 /usr/bin/php-cgi
...

RSS is the Real-memory (resident set) size in kilobytes of the process.

To sum it all up in bash (a bit rusty sorry)

#!/bin/bash

total=0
for i in `ps -C php-cgi -o rss=`
do
    total=$(($total + $i))
done
echo "Memory usage: $total kb"

# Output: Memory usage: 4540 kb

One liner:

total=0; for i in `ps -C php-cgi -o rss=`; do total=$(($total+$i)); done; echo "Memory usage: $total kb";

I know the reliability of the memory part in ps is questioned but at least it gives you an idea of what the usage is like.

你怎么这么可爱啊 2024-08-01 22:13:22

除了上面显示的内置命令之外,您还可以使用 XHProf 进行分析您的脚本和 XHGui 用于在漂亮的浏览器应用程序中显示分析结果。 您可以深入了解您的方法如何使用内存以及应用程序中内存使用的峰值。

Besides the build-in commands shown above, you can use XHProf for profiling your scripts and XHGui for showing profiling results in a nice browser application. You get in-depth information on how your methods use memory and what are the peaks of memory usage within your application.

作死小能手 2024-08-01 22:13:22

http://php.net/manual/en/function.memory- get-usage.php

应该为您提供线程在脚本本身中使用的内存量。 我认为因为脚本(和线程)最多只存在几毫秒 - 只是生成页面所需的时间 - 在 PHP 之外捕获它可能很困难。

  • 计划 B

您还可以从服务器获取可能更准确的调试信息 - 我个人使用 xdebug,当它抛出错误/通知时,它会为您提供堆栈跟踪、时间和内存使用情况。 您可以在脚本末尾使用以下命令触发它:

trigger_error ('Finished', E_USER_NOTICE);

它会给您信息。 我不确定是否能捕获数据 - 如果您需要的话,文档中可能有一个关于如何捕获数据的函数 - 我隐约记得看到过一个。

http://php.net/manual/en/function.memory-get-usage.php

Should give you the amount of memory that the thread is using from within the script itself. I think because the script (and thread) only exists for a few milliseconds at most - just the time it takes to generate the page - catching it outside PHP might be difficult.

  • Plan B

You can also get debugging information from the server that may be more accurate - I use xdebug personally, and when it throws an error/notice it gives you a stack trace, time and memory usage. You can trigger it at the end of the script with:

trigger_error ('Finished', E_USER_NOTICE);

And it'll give you the info. I am not sure at catching the data - if you need to there may be a function in the docs on how - I vaguely remember seeing one.

一页 2024-08-01 22:13:21

PHP 有它自己的内存测试功能,我不知道这对你是否有任何用处,但如果你只是想记录它,你可以使用:
http://php.net/manual/en/function.memory -get-peak-usage.php

    echo "Using ", memory_get_peak_usage(1), " bytes of ram.";

PHP has it's own memory testing functions, I don't know if that's any use to you, but if you just want to log it you could use:
http://php.net/manual/en/function.memory-get-peak-usage.php

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