用于直观分析 PHP 应用程序内存使用情况的工具

发布于 2024-07-07 21:47:12 字数 245 浏览 8 评论 0原文

是否有任何免费软件或商业软件可以帮助分析 PHP 应用程序的内存使用情况? 我知道 xdebug 可以生成跟踪文件,通过函数调用显示内存使用情况,但如果没有图形工具,数据很难解释。

理想情况下,我希望不仅能够查看总内存使用情况,还能够查看堆上有哪些对象以及谁引用了它们,类似于 Jprofiler

Is there anything out there freeware or commercial that can facilitate analysis of memory usage by a PHP application? I know xdebug can produce trace files that shows memory usage by function call but without a graphical tool the data is hard to interpret.

Ideally I would like to be able to view not only total memory usage but also what objects are on the heap and who references them similar to Jprofiler.

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

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

发布评论

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

评论(8

撩人痒 2024-07-14 21:47:12

您可能知道,自 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-14 21:47:12

我最近遇到了同样的问题,不幸的是找不到任何特定的工具。

但有帮助的是,以人类可读的格式输出 xdebug 跟踪,并启用 mem 增量(INI 设置,xdebug.show_mem_deltas 或者我认为的东西?)。 然后对输出运行 sort(如果您在 *nix 上):

sort -bgrk 3 -o sorted.txt mytracefile.xt 

对第三列(mem 增量)进行排序。 您还可以对第二列进行排序,在这种情况下,您可以找到应用程序总共使用内存最多的行。

当然,这无法检测对象的内存使用量何时仅以小幅增量增加,但最终总体上使用了大量内存。 我有一个相当愚蠢的方法,尝试使用对象迭代和序列化的组合来做到这一点。 它可能并不完全等同于内存使用情况,但希望能够给出从哪里开始寻找的想法。 请记住,它本身会耗尽内存,并且还没有经过广泛的测试,因此购买者请注意:

function analyzeMem($obj, $deep=false)
{
    if (!is_scalar($obj))
    {
        $usage = array('Total'=>strlen(serialize($obj)));
        while (list($prop, $propVal) = each($obj)) 
        {
            if ($deep && (is_object($propVal) || is_array($propVal)))
            {
                $usage['Children'][$prop] = analyzeMem($propVal);
            }
            else
            {
                $usage['Children'][$prop] = strlen(serialize($propVal));
            }
        }
        return $usage;
    }
    else
    {
        return strlen(serialize($obj));
    }
}

print_r(analyzeMem(get_defined_vars()));

另外,刚刚得到同事建议的这种方法(丹尼斯欢呼;-)这隐藏了低于 2 级缩进的步骤,您可以很容易地看到总体内存使用量上升的点,并且可以通过增加缩进来缩小范围:

egrep '[0-9]+ (  ){1,2}-> ' mytracefile.xt

I came across the same issue recently, couldn't find any specific tools unfortunately.

But something that helped was to output the xdebug trace in human readable format with mem deltas enabled (an INI setting, xdebug.show_mem_deltas or something I think?). Then run sort (if you are on *nix) on the output:

sort -bgrk 3 -o sorted.txt mytracefile.xt 

That sorts on the third col, the mem deltas. You can also sort on the second column, in which case you can find the line at which your app uses the most memory in total.

Of course, this can't detect when an object's memory usage only creeps up in small increments but ends up using a lot of memory overall. I have a fairly dumb method that attempts to do this using a combination of object iteration and serialization. It probably doesn't equate exactly to memory usage, but hopefully gives an idea of where to start looking. Bear in mind it will use up memory itself, and also has not been extensively tested, so buyer beware:

function analyzeMem($obj, $deep=false)
{
    if (!is_scalar($obj))
    {
        $usage = array('Total'=>strlen(serialize($obj)));
        while (list($prop, $propVal) = each($obj)) 
        {
            if ($deep && (is_object($propVal) || is_array($propVal)))
            {
                $usage['Children'][$prop] = analyzeMem($propVal);
            }
            else
            {
                $usage['Children'][$prop] = strlen(serialize($propVal));
            }
        }
        return $usage;
    }
    else
    {
        return strlen(serialize($obj));
    }
}

print_r(analyzeMem(get_defined_vars()));

Also, just got suggested this method by a colleague (cheers Dennis ;-) This hides the steps that are below 2 levels of indentation, you can quite easily see the points where the overall memory usage jumps up, and can narrow things down by increasing the indentation:

egrep '[0-9]+ (  ){1,2}-> ' mytracefile.xt
囍孤女 2024-07-14 21:47:12

在 Xdebug 2.0.4 的 http://www.xdebug.org/updates.php 上,他们写道在“删除的功能”部分:“...删除了对内存分析的支持,因为它无法正常工作...”。 因此 xdebug 不会成为一个选项

On http://www.xdebug.org/updates.php for Xdebug 2.0.4 they write in section "removed functions": "...Removed support for Memory profiling as that didn't work properly...". Hence xdebug wont be an option

你是我的挚爱i 2024-07-14 21:47:12

我个人使用 https://github.com/arnaud-lb/php-memory-profiler

在 PHP 5.6 和 Ubuntu 18 上,以及用于可视化的 Kcachegrind。

Kcachegrind 还可以,但不是最好的。 我希望找到更好的替代方案,即使是在 Mac 或 Windows 上。

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

on PHP 5.6 and Ubuntu 18, and Kcachegrind for visualizing.

Kcachegrind is okay, but not the best. I hope to find a better alternative even if it's on Mac or Windows.

奶气 2024-07-14 21:47:12

在 2018 年 1 月 29 日发布的 2.6.0 版本中,xdebug 添加了对分析内存使用情况的支持。 现在您可以生成包含时间和内存信息的 callgrind 文件。 在 Mac 上,您可以使用 Qcachegrind 或 Profiling Viewer(高级版)可视化该信息。

分析查看器调用图

With the version 2.6.0 on 2018-01-29 xdebug added support for profiling memory usage. Now you can generate callgrind files with time and memory information. On Mac you can visualize that information for example with Qcachegrind or Profiling Viewer (premium).

Profiling Viewer callgraph

明媚如初 2024-07-14 21:47:12

用于 xdebug 输出的图形工具是 KCacheGrind

A graphical tool for xdebug output is KCacheGrind.

给不了的爱 2024-07-14 21:47:12

尝试 webgrind。 它以易于阅读、基于浏览器的格式为您提供 CacheGrinder 的分析。 我使用的是 Mac,它使分析变得轻而易举。

Try webgrind. It gives you the profiling of CacheGrinder in an easy to read, browser based format. I'm on a Mac and it has made profiling a breeze.

娜些时光,永不杰束 2024-07-14 21:47:12

phpDesigner 2008 可以使用 xdebug 和 KCacheGrind 对网站进行调试和基准测试。 它还具有内置监视器。

phpDesigner 2008 can debug and benchmark websites using xdebug and KCacheGrind. It also has a built-in monitor.

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