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?
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().
memory_get_usage() — Returns the amount of memory allocated to PHP.
The sizeof() function returns the number of elements in an array. The sizeof() function is an alias of the count() function.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With