php 如何在linux中监控php的内存使用情况?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1072975/
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
How can I monitor memory usage of php in linux?
提问by DEzra
I have used valgrinds massif tool to monitor memory usage in the past.
我过去曾使用 valgrinds massif 工具来监控内存使用情况。
Does anyone know how to capture memory use of php processes that are spawned on a linux lighttpd server?
有谁知道如何捕获在 linux lighttpd 服务器上产生的 php 进程的内存使用情况?
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)
我发现 Valgrind 无法附加到预运行进程(无论如何我都不知道 php 进程的 PID)
I only see lighttpd's memory usage, not the PHP processes that are spawned by lighttpd cgi module.
我只看到 lighttpd 的内存使用情况,而不是 lighttpd cgi 模块产生的 PHP 进程。
Thanks in advance.
提前致谢。
采纳答案by scragar
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
PHP有它自己的内存测试功能,我不知道这对你有没有用,但如果你只是想记录它,你可以使用:http: //php.net/manual/en/function.memory-get-高峰使用率.php
echo "Using ", memory_get_peak_usage(1), " bytes of ram.";
回答by Palm
Can't you use the 'ps' tool?
你不能使用'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 is the Real-memory (resident set) size in kilobytes of the process.
RSS 是进程的实际内存(驻留集)大小(以千字节为单位)。
To sum it all up in bash (a bit rusty sorry)
用 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
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 psis questioned but at least it gives you an idea of what the usage is like.
我知道ps 中内存部分的可靠性受到质疑,但至少它让您了解使用情况。
回答by Michael Lihs
Besides the build-in commands shown above, you can use XHProffor profiling your scripts and XHGuifor 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.
除了上面显示的内置命令,您还可以使用XHProf来分析您的脚本,并使用XHGui在一个漂亮的浏览器应用程序中显示分析结果。您将获得有关您的方法如何使用内存以及您的应用程序中内存使用高峰是什么的深入信息。
回答by Meep3D
http://php.net/manual/en/function.memory-get-usage.php
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.
应该为您提供线程在脚本本身中使用的内存量。我认为因为脚本(和线程)最多只存在几毫秒 - 只是生成页面所需的时间 - 在 PHP 之外捕获它可能很困难。
- Plan B
- 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:
您还可以从服务器获取可能更准确的调试信息 - 我个人使用 xdebug,当它抛出错误/通知时,它会为您提供堆栈跟踪、时间和内存使用情况。您可以在脚本结束时使用以下命令触发它:
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.
它会给你信息。我不确定捕获数据 - 如果您需要,文档中可能有一个关于如何捕获的功能 - 我依稀记得看到过一个。

