How to find memory used by an object in PHP? (sizeof)
Solution 1
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.
Solution 2
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.
Solution 3
If you need to know the size of an already created object or array, you can use the following code to find it out.
<?php
function rec_copy($src) {
if (is_string($src)) {
return str_replace('SOME_NEVER_OCCURING_VALUE_145645645734534523', 'XYZ', $src);
}
if (is_numeric($src)) {
return ($src + 0);
}
if (is_bool($src)) {
return ($src?TRUE:FALSE);
}
if (is_null($src)) {
return NULL;
}
if (is_object($src)) {
$new = (object) array();
foreach ($src as $key => $val) {
$new->$key = rec_copy($val);
}
return $new;
}
if (!is_array($src)) {
print_r(gettype($src) . "\n");
return $src;
}
$new = array();
foreach ($src as $key => $val) {
$new[$key] = rec_copy($val);
}
return $new;
}
$old = memory_get_usage();
$dummy = rec_copy($src);
$mem = memory_get_usage();
$size = abs($mem - $old);
?>
This essentially creates a copy of the array structure and all of its members.
A not 100% accurate, but still working version is also:
<?php
$old = memory_get_usage();
$dummy = unserialize(serialize($src));
$mem = memory_get_usage();
$size = abs($mem - $old);
Hope that helps for cases where the object is already build.
erotsppa
Updated on June 25, 2021Comments
-
erotsppa almost 3 years
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?
-
NickT about 13 yearsYou forgot to wait for April 1st to post this one mate!
-
Stann about 13 yearsi don't think he was serious:)
-
danorton almost 13 yearsThat might have some uses, but not for memory. PHP <= 5.2 is profoundly inefficient in the way that it stores some memory structures.
-
Mathieu Dumoulin almost 12 yearsAnother way to do it while in memory is to get the memory usage, clone the object, get the new memory usage and unset the clone. There is a footprint though, so don't abuse... :)
-
Neocortex over 10 yearsI believe the result that we are getting from the
echo memory_get_usage()
before and after an array, its units suppose to be inKB
!!! I divided the memory size by 1024! To get actual memory size consumed. -
Alex about 9 yearsNot so insane thought. I think what Peter said could be turned into something functional...probably a recursive function that will gradually go deeper into the object and collect and sums all the size of variables. Could be useful for debug purposes....
-
Alex about 9 yearsYeah? Well, what if PHP decides to play with the memory just before calling the second memory_get_usage?...very brittle solution....
-
userfuser almost 9 yearsThis object cloning principle may not work so well for smaller objects. When I tested a bit, my PHP seemed to be acquiring new pieces of RAM only in chunks of 256 KB, or at least that was what I received from memory_get_usage(). So, if your object would be 20KB, you'd need to create cca. 10 of them, just to get the memory_get_usage() to change.
-
userfuser almost 9 yearsFar from insane or a joke, but still very imprecise unless done thoroughly and properly. Anyway, it could easily get you very good idea of what is eating your RAM the most (if some var is spending 40% or more of your RAM - you would notice it with this)
-
clod986 about 8 yearsI was using this to check the json response before sending it, but it actually exhausts my memory limit
-
Platinum Fire over 7 yearsThis answer has been debunked on multiple questions already, as memory_get_usage() gets the used memory of the entire PHP script and does not separate things like overheads, ergo comparing it's results does not give you the size of the object.
-
Yousha Aleayoub about 6 yearsactually
memory_get_usage(FALSE)
or you will get allocated memory -
Himanshu Upadhyay almost 6 yearsthis will give size in bytes or what?
-
fede72bari over 2 yearsif you want to detect which is the object that takes the system memory over the limit, your process double even if temporarily the used amount of memory pushing the system even before overload
-
fede72bari over 2 yearsif you want to detect which is the object that takes the system memory over the limit, your process double even if temporarily the used amount of memory pushing the system even before overload