问题描述
PHP(或PHP扩展)中是否有一个函数可以找出给定变量使用了多少内存? sizeof
只是告诉我元素/属性的数量.
memory_get_usage
可以帮助我给整个脚本使用的内存大小.有没有办法对单个变量执行此操作?
请注意,这是在开发机器上,因此可以加载扩展程序或调试工具.
您可能需要Memory Profiler.我已经收集了一些信息,但是我复制了一些可能对您有帮助的重要信息.
您可能已经知道,自2. *版本以来,Xdebug放弃了对内存分析的支持.请在此处搜索已删除的功能"字符串: http://www.xdebug.org/updates.php
其他探查器选项
php-memory-profiler
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
打开callgrind.out
文件使用Google gperftools(推荐!)
首先通过在此处下载最新的软件包来安装 Google gperftools : https://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 将在您现有的浏览器会话中创建一个新窗口,如下所示:
Xhprof + Xhgui(我认为这是同时分析CPU和内存的最佳选择)
使用 Xhprof 和 Xhgui ,您还可以分析cpu的使用情况,也可以仅分析内存使用情况(如果这是当前的问题).这是一个非常完整的解决方案,它使您可以完全控制,并且日志可以写在mongo或文件系统中.
有关更多详细信息,请参见此处. /p>
黑火
Blackfire是由Symfony2伙计SensioLabs进行的PHP分析器, https://blackfire.io/
如果您使用 puphpet 来设置您的虚拟机,您将很高兴知道它受支持;-)
Xdebug和跟踪内存使用情况
XDEBUG2 是PHP的扩展. Xdebug允许您记录所有函数调用,包括参数和以不同格式返回到文件的值.共有三种输出格式.一个被定义为人类可读的跟踪,另一个则更易于解析,更适合于计算机程序,而最后一个则使用HTML格式化跟踪.您可以使用该设置在两种不同的格式之间切换.例如在此处可用
forp
forp 简单,非侵入性,面向生产的PHP分析器.一些功能是:
-
时间测量和每个功能分配的内存
-
CPU使用率
-
函数调用的文件和行号
-
以Google的跟踪事件格式输出
-
功能说明
-
功能分组
-
函数别名(对匿名函数有用)
DBG
DBG 是功能齐全的php调试器,它是一个交互式工具,可帮助您调试php脚本.它可以在生产和/或开发的WEB服务器上运行,并允许您从IDE或控制台本地或远程调试脚本,其功能包括:
-
远程和本地调试
-
显式和隐式激活
-
调用堆栈,包括函数调用,动态和静态方法调用及其参数
-
在调用堆栈中进行导航,并能够评估相应(嵌套)位置中的变量
-
进入/退出/越过/运行到光标功能
-
条件断点
-
全局断点
-
记录错误和警告
-
多个并行会话进行并行调试
-
对GUI和CLI前端的支持
-
支持IPv6和IPv4网络
-
调试器传输的所有数据都可以选择使用SSL保护
Is there a function in PHP (or a PHP extension) to find out how much memory a given variable uses? sizeof
just tells me the number of elements/properties.
memory_get_usage
helps in that it gives me the memory size used by the whole script. Is there a way to do this for a single variable?
Note that this is on a development machine, so loading extensions or debug tools is feasible.
You Probably need a Memory Profiler. I have gathered information fro SO but I have copied the some important thing which may help you also.
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
Other Profiler Options
php-memory-profiler
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:
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 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 and tracing memory usage
XDEBUG2 is a extension for PHP. Xdebug allows you to log all function calls, including parameters and return values to a file in different formats.There are three output formats. One is meant as a human readable trace, another one is more suited for computer programs as it is easier to parse, and the last one uses HTML for formatting the trace. You can switch between the two different formats with the setting. An example would be available here
forp
forp simple, non intrusive, production-oriented, PHP profiler. Some of features are:
measurement of time and allocated memory for each function
CPU usage
file and line number of the function call
output as Google's Trace Event format
caption of functions
grouping of functions
aliases of functions (useful for anonymous functions)
DBG
DBG is a a full-featured php debugger, an interactive tool that helps you debugging php scripts. It works on a production and/or development WEB server and allows you debug your scripts locally or remotely, from an IDE or console and its features are:
Remote and local debugging
Explicit and implicit activation
Call stack, including function calls, dynamic and static method calls, with their parameters
Navigation through the call stack with ability to evaluate variables in corresponding (nested) places
Step in/Step out/Step over/Run to cursor functionality
Conditional breakpoints
Global breakpoints
Logging for errors and warnings
Multiple simultaneous sessions for parallel debugging
Support for GUI and CLI front-ends
IPv6 and IPv4 networks supported
All data transferred by debugger can be optionally protected with SSL
这篇关于如何确定变量的内存占用量(大小)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!