問題描述
PHP(或 PHP 擴展)中是否有函數可以找出給定變量使用了多少內存?sizeof
只是告訴我元素/屬性的數量.
memory_get_usage
的幫助在于它為我提供了 整個 腳本使用的內存大小.有沒有辦法對單個變量執行此操作?
請注意,這是在開發機器上,因此加載擴展或調試工具是可行的.
您可能需要一個內存分析器.我已經收集了 SO 的信息,但我復制了一些可能對您也有幫助的重要內容.
您可能知道,從 2.* 版本開始,Xdebug 放棄了內存分析支持.請搜索已刪除的功能"這里的字符串:.
黑火
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
Removed functions
Removed support for Memory profiling as that didn't work properly.
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
這篇關于如何確定變量的內存占用(大小)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!