PHP 内存分析

发布于 2024-07-22 07:11:42 字数 282 浏览 5 评论 0原文

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

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

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

还有别的事吗?

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.

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

  • xdebug does provide 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.

Is there anything else?

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

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

发布评论

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

评论(4

云淡月浅 2024-07-29 07:11:42

您可能知道,自 2.* 版本以来,Xdebug 放弃了内存分析支持。 请在此处搜索“已删除的函数”字符串:http://www.xdebug.org/updates.php< /a>

删除的功能

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

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

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

这是我在 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

然后在我的代码中:

<?php

memprof_enable();

// do your stuff

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

最后使用 KCachegrind

使用 Google gperftools(推荐!)

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

然后一如既往:

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

现在在您的代码中:

memprof_enable();

// do your magic

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

然后打开终端并启动:

pprof --web /tmp/profile.heap

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

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

Xhprof + Xhgui (我认为最好的分析 cpu 和内存的方法)

使用 XhprofXhgui 你可以分析 cpu以及使用情况,或者只是内存使用情况(如果这是您目前的问题)。
这是一个非常完整的解决方案,它为您提供完全的控制权,并且日志可以写入 mongo 或文件系统中。

有关更多详细信息请在此处查看我的答案

Blackfire

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

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

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

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

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

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.out file with KCachegrind

Using Google gperftools (recommended!)

First of all install the Google gperftools by downloading the latest package here: https://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

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

PHP memory profiling with memprof and gperftools

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

With Xhprof and Xhgui you 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.

For more details see my answer here.

Blackfire

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

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

纸伞微斜 2024-07-29 07:11:42

嗯,这可能不完全是您想要的,但是 PHP 确实有几个内置函数可以输出内存使用情况。 如果您只是想查看函数调用使用了多少内存,您可以在函数调用之前和之后使用 memory_get_peak_usage()打电话,拿差价。

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

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

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.

You use the same technique around your data using the very similar 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.

つ可否回来 2024-07-29 07:11:42

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

从文档中:

自 Xdebug 2.6 起,分析器还收集有关使用了多少内存以及哪些函数增加了内存使用量的信息。

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

qcachegrind 示例

Xdebug reimplemented 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 :)

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.

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 sample

世界如花海般美丽 2024-07-29 07:11:42

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

我使用的是 Mac,所以如果你使用的是 Windows,你会必须测试这个,但这对我有用。

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

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

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

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

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.

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.

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

Don't forget to chmod this file to 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.

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