php PHP内存分析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/880458/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:12:01  来源:igfitidea点击:

PHP memory profiling

phpmemoryprofiling

提问by JW.

What's a good way to profile a PHP page's memory usage? For example, to see how much memory my data is using, and/or which function calls are allocating the most memory.

分析 PHP 页面内存使用情况的好方法是什么?例如,查看我的数据使用了多少内存,和/或哪些函数调用分配了最多的内存。

  • xdebug doesn't seem to provide memory information in its profiling feature.

  • xdebug doesprovide it in its tracing feature. This is pretty close to what I want, except the sheer amount of data is overwhelming, since it shows memory deltas for every single function call. If it were possible to hide calls below a certain depth, maybe with some GUI tool, that would solve my problem.

  • xdebug 似乎没有在其分析功能中提供内存信息。

  • xdebug确实在其跟踪功能提供了它。这与我想要的非常接近,除了庞大的数据量是压倒性的,因为它显示了每个函数调用的内存增量。如果可以将调用隐藏在一定深度以下,也许使用一些 GUI 工具,那将解决我的问题。

Is there anything else?

还有别的事吗?

采纳答案by SeanDowney

Xdebugreimplemented memory tracing in 2.6(2018-01-29) which can be used in Qcachegrind or similar tool. Just make sure to select the memory option:)

Xdebug在 2.6(2018-01-29) 中重新实现了内存跟踪,可用于 Qcachegrind 或类似工具。只需确保选择内存选项:)

From the docs:

从文档:

Since Xdebug 2.6, the profiler also collects information about how much memory is being used, and which functions aGnd methods increased memory usage.

从 Xdebug 2.6 开始,分析器还收集有关正在使用多少内存的信息,以及哪些函数 aGnd 方法增加了内存使用量。

I'm not familiar with the format of the file, but it's Qcachegrind has worked great for me in tracing a couple memory issues.

我不熟悉文件的格式,但 Qcachegrind 在跟踪几个内存问题方面对我很有帮助。

qcachegrind sample

qcachegrind 示例

回答by Francesco Casula

As you probably know, Xdebug dropped the memory profiling support since the 2.* version. Please search for the "removed functions" string here: http://www.xdebug.org/updates.php

您可能知道,从 2.* 版本开始,Xdebug 放弃了内存分析支持。请在此处搜索“已删除的功能”字符串:http: //www.xdebug.org/updates.php

Removed functions

Removed support for Memory profiling as that didn't work properly.

删除的功能

删除了对内存分析的支持,因为它无法正常工作。

So I've tried another tool and it worked well for me.

所以我尝试了另一种工具,它对我来说效果很好。

https://github.com/arnaud-lb/php-memory-profiler

https://github.com/arnaud-lb/php-memory-profiler

This is what I've done on my Ubuntu server to enable it:

这是我在我的 Ubuntu 服务器上为启用它所做的:

sudo apt-get install libjudy-dev libjudydebian1
sudo pecl install memprof
echo "extension=memprof.so" > /etc/php5/mods-available/memprof.ini
sudo php5enmod memprof
service apache2 restart

And then in my code:

然后在我的代码中:

<?php

memprof_enable();

// do your stuff

memprof_dump_callgrind(fopen("/tmp/callgrind.out", "w"));

Finally open the callgrind.outfile with KCachegrind

最后callgrind.outKCachegrind打开文件

Using Google gperftools (recommended!)

使用 Google gperftools(推荐!)

First of all install the Google gperftoolsby downloading the latest package here: https://code.google.com/p/gperftools/

首先通过在此处下载最新软件包来安装Google gperftoolshttps: //code.google.com/p/gperftools/

Then as always:

然后一如既往:

sudo apt-get update
sudo apt-get install libunwind-dev -y
./configure
make
make install

Now in your code:

现在在您的代码中:

memprof_enable();

// do your magic

memprof_dump_pprof(fopen("/tmp/profile.heap", "w"));

Then open your terminal and launch:

然后打开你的终端并启动:

pprof --web /tmp/profile.heap

pprofwill create a new window in your existing browser session with something like shown below:

pprof将在您现有的浏览器会话中创建一个新窗口,如下所示:

PHP memory profiling with memprof and gperftools

使用 memprof 和 gperftools 进行 PHP 内存分析

Xhprof + Xhgui (the best in my opinion to profile both cpu and memory)

Xhprof + Xhgui(我认为最好的同时配置 CPU 和内存)

With Xhprofand Xhguiyou can profile the cpu usage as well or just the memory usage if that's your issue at the moment. It's a very complete solutions, it gives you full control and the logs can be written both on mongo or in the filesystem.

使用XhprofXhgui,您还可以分析 cpu 使用情况,或者如果这是您目前的问题,则仅分析内存使用情况。这是一个非常完整的解决方案,它给你完全的控制权,日志可以写在 mongo 或文件系统中。

For more details see my answer here.

有关更多详细信息,请参阅我的回答

Blackfire

黑火

Blackfire is a PHP profiler by SensioLabs, the Symfony2 guys https://blackfire.io/

Blackfire 是 Symfony2 的 SensioLabs 开发的 PHP 分析器https://blackfire.io/

If you use puphpetto set up your virtual machine you'll be happy to know it's supported ;-)

如果你使用puphpet来设置你的虚拟机,你会很高兴知道它是受支持的 ;-)

回答by zombat

Well, this may not be exactly what you're looking for, but PHP does have a couple of functions built-in that will output memory usage. If you just wanted to see how much memory a function call is using, you could use memory_get_peak_usage()before and after a call, and take the difference.

好吧,这可能不是您正在寻找的内容,但是 PHP 确实有一些内置函数可以输出内存使用情况。如果您只想查看函数调用使用了多少内存,您可以在调用之前和之后使用memory_get_peak_usage()并计算差异。

You use the same technique around your data using the very similar memory_get_usage().

您可以使用非常相似的memory_get_usage()围绕您的数据使用相同的技术。

Pretty unsophisticated approach, but it's a quick way to check out a piece of code. I agree that xdebug mem deltas can be too verbose to be useful sometimes, so I often just use it to narrow down to a section of code, then dump out specific memory usage for small pieces manually.

相当简单的方法,但它是检查一段代码的快速方法。我同意 xdebug mem deltas 有时可能过于冗长而无用,所以我经常只是使用它来缩小到一段代码,然后手动转储小块的特定内存使用情况。

回答by 2upmedia

http://geek.michaelgrace.org/2012/04/tracing-php-memory-usage-using-xdebug-and-mamp-on-mac/

http://geek.michaelgrace.org/2012/04/tracing-php-memory-usage-using-xdebug-and-mamp-on-mac/

I'm on a Mac so if you're on Windows you'll have to test this, but this works for me.

我在 Mac 上,所以如果你在 Windows 上,你必须测试这个,但这对我有用。

I modified my tracefile-analyzer.php file and added the path to the PHP binary at the top so that you could call it in terminal as a normal unix script.

我修改了我的 tracefile-analyzer.php 文件并在顶部添加了 PHP 二进制文件的路径,以便您可以在终端中将它作为普通的 unix 脚本调用。

#!/Applications/MAMP/bin/php5.3/bin/php
<?php
if ( $argc <= 1 || $argc > 4 )
{

Don't forget to chmod this file to 755.

不要忘记将此文件 chmod 为 755。

You could easily create a ruby watchr script to automatically call the script each time it creates a memory profile file (*.xt). That way you could keep testing and seeing your improvements without having to execute the command over and over.

您可以轻松创建一个 ruby​​ watchr 脚本,以便在每次创建内存配置文件 (*.xt) 时自动调用该脚本。这样你就可以继续测试并看到你的改进,而不必一遍又一遍地执行命令。