Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

memory

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.

like image 674
Piskvor left the building Avatar asked Feb 03 '10 14:02

Piskvor left the building


People also ask

How do you find the memory size of a variable in python?

Use sys. getsizeof to get the size of an object, in bytes.

Which function is used to detect how much memory the variable uses?

memory_get_usage() — Returns the amount of memory allocated to PHP.

How do I check memory usage in python?

You can use it by putting the @profile decorator around any function or method and running python -m memory_profiler myscript. You'll see line-by-line memory usage once your script exits.


2 Answers

There's no direct way to get the memory usage of a single variable, but as Gordon suggested, you can use memory_get_usage. That will return the total amount of memory allocated, so you can use a workaround and measure usage before and after to get the usage of a single variable. This is a bit hacky, but it should work.

$start_memory = memory_get_usage(); $foo = "Some variable"; echo memory_get_usage() - $start_memory; 

Note that this is in no way a reliable method, you can't be sure that nothing else touches memory while assigning the variable, so this should only be used as an approximation.

You can actually turn that to an function by creating a copy of the variable inside the function and measuring the memory used. Haven't tested this, but in principle, I don't see anything wrong with it:

function sizeofvar($var) {     $start_memory = memory_get_usage();     $tmp = unserialize(serialize($var));     return memory_get_usage() - $start_memory; } 
like image 123
Tatu Ulmanen Avatar answered Sep 18 '22 09:09

Tatu Ulmanen


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:

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

like image 43
Vineet1982 Avatar answered Sep 19 '22 09:09

Vineet1982