How to determine the memory footprint (size) of a variable?

后端 未结 11 2386
有刺的猬
有刺的猬 2020-11-28 19:30

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.

11条回答
  •  攒了一身酷
    2020-11-28 20:09

    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:

    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 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

提交回复
热议问题