Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find memory used by an object in PHP? (sizeof)

How to find memory used by an object in PHP? (c's sizeof). The object I want to find out about is a dictionary with strings and ints in it so it makes it hard to calculate it manually. Also string in php can be of varied length depending on encoding (utf8 etc) correct?

like image 489
erotsppa Avatar asked Jul 02 '09 16:07

erotsppa


People also ask

How do I monitor PHP memory usage?

The memory_get_usage function can be used to track the memory usage. The 'malloc' function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally. The above mentioned memory usage can be tracked using memory_get_usage().

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

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

What is sizeof in PHP?

The sizeof() function returns the number of elements in an array. The sizeof() function is an alias of the count() function.

What is memory limit in PHP INI?

The default memory limit is 256M and this is usually more than sufficient for most needs. If you need to raise this limit, you must create a phprc file.


2 Answers

You could use memory_get_usage().

Run it once before creating your object, then again after creating your object, and take the difference between the two results.

like image 134
zombat Avatar answered Sep 22 '22 20:09

zombat


To get an idea about the objects size, try

strlen(serialize($object)); 

It is by no means accurate, but an easy way to get a number for comparison.

like image 41
rekowski Avatar answered Sep 23 '22 20:09

rekowski