PHP 内存分析
分析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能知道,自 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 ;-)
嗯,这可能不完全是您想要的,但是 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.
Xdebug 在2.6中重新实现了内存跟踪(2018-01-29),可以在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:
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.
http:// geek.michaelgrace.org/2012/04/tracing-php-memory-usage-using-xdebug-and-mamp-on-mac/
我使用的是 Mac,所以如果你使用的是 Windows,你会必须测试这个,但这对我有用。
我修改了我的tracefile-analyzer.php 文件,并在顶部添加了 PHP 二进制文件的路径,以便您可以在终端中将其作为普通的 unix 脚本调用。
不要忘记将此文件 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.
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.