用于直观分析 PHP 应用程序内存使用情况的工具
是否有任何免费软件或商业软件可以帮助分析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可能知道,自 2.* 版本以来,Xdebug 放弃了内存分析支持。 请在此处搜索“已删除的函数”字符串: http://www.xdebug.org/updates.php< /a>
所以我尝试了另一种工具,它对我来说效果很好。
https://github.com/arnaud-lb/php-memory-profiler
这是我在 Ubuntu 服务器上启用它的方法:
然后在我的代码中:
最后使用 KCachegrind
使用 Google gperftools(推荐!)
首先,通过在此处下载最新的软件包来安装 Google gperftools:https://code.google.com/p/gperftools/
然后一如既往:
现在在您的代码中:
然后打开终端并启动:
pprof 将在现有浏览器会话中创建一个新窗口,如下所示:
Xhprof + Xhgui (我认为最好的分析 cpu 和内存的方法)
使用 Xhprof 和 Xhgui 你可以分析 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
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:
And then in my code:
Finally open the
callgrind.out
file with KCachegrindUsing 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:
Now in your code:
Then open your terminal and launch:
pprof will create a new window in your existing browser session with something like shown below:
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 ;-)
我最近遇到了同样的问题,不幸的是找不到任何特定的工具。
但有帮助的是,以人类可读的格式输出 xdebug 跟踪,并启用 mem 增量(INI 设置,xdebug.show_mem_deltas 或者我认为的东西?)。 然后对输出运行 sort(如果您在 *nix 上):
对第三列(mem 增量)进行排序。 您还可以对第二列进行排序,在这种情况下,您可以找到应用程序总共使用内存最多的行。
当然,这无法检测对象的内存使用量何时仅以小幅增量增加,但最终总体上使用了大量内存。 我有一个相当愚蠢的方法,尝试使用对象迭代和序列化的组合来做到这一点。 它可能并不完全等同于内存使用情况,但希望能够给出从哪里开始寻找的想法。 请记住,它本身会耗尽内存,并且还没有经过广泛的测试,因此购买者请注意:
另外,刚刚得到同事建议的这种方法(丹尼斯欢呼;-)这隐藏了低于 2 级缩进的步骤,您可以很容易地看到总体内存使用量上升的点,并且可以通过增加缩进来缩小范围:
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:
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:
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:
在 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
我个人使用 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.
在 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).
用于 xdebug 输出的图形工具是 KCacheGrind。
A graphical tool for xdebug output is KCacheGrind.
尝试 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.
phpDesigner 2008 可以使用 xdebug 和 KCacheGrind 对网站进行调试和基准测试。 它还具有内置监视器。
phpDesigner 2008 can debug and benchmark websites using xdebug and KCacheGrind. It also has a built-in monitor.