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?
6 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.

- 92,731
- 24
- 156
- 164
-
2Another 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... :) – Mathieu Dumoulin Jun 22 '12 at 14:43
-
I believe the result that we are getting from the `echo memory_get_usage()` before and after an array, its units suppose to be in `KB`!!! I divided the memory size by 1024! To get actual memory size consumed. – Neocortex Dec 27 '13 at 06:27
-
2Yeah? Well, what if PHP decides to play with the memory just before calling the second memory_get_usage?...very brittle solution.... – Alex Apr 08 '15 at 08:22
-
1This 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 Jul 09 '15 at 08:09
-
2This 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. – Platinum Fire Sep 20 '16 at 10:28
-
actually `memory_get_usage(FALSE)` or you will get allocated memory – Yousha Aleayoub Apr 18 '18 at 07:32
-
This answer doesn't account for any changes made to the object after initialisation. For example, the object may have an array field that is added to by various method calls, possibly burried in various injected objects. – Dezza Aug 26 '22 at 10:56
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.

- 159
- 1
- 4
-
1That might have some uses, but not for memory. PHP <= 5.2 is profoundly inefficient in the way that it stores some memory structures. – danorton Aug 23 '11 at 20:45
-
I was using this to check the json response before sending it, but it actually exhausts my memory limit – clod986 May 04 '16 at 16:46
-
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.

- 279
- 2
- 5
-
if 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 Sep 15 '21 at 12:44
This method converts the array to a json string and determines the length of that string. The result should be fairly similar to the size of the array (both will have delimiters to partition members of the array or the stringified json)
strlen(json_encode(YourArray))
this method could be help you:
function getVariableUsage($var) {
$total_memory = memory_get_usage();
$tmp = unserialize(serialize($var));
return memory_get_usage() - $total_memory;
}
$var = "Hey, what's you doing?";
echo getVariableUsage($var);

- 358
- 1
- 3
- 13
-
if 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 Sep 15 '21 at 12:44
I don't know that there is a simple way to get the size of an object in PHP. You might just have to do an algorith that
- Counts the ints
- Multiplies number of ints by size of an int on hard disk
- Convert characters in strings to ASCII and
- Multiply the ASCII values by how much they take up on disk
I'm sure there is a better way, but this would work, even though it would be a pain.

- 421
- 2
- 8
-
18
-
-
1Not 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 Apr 08 '15 at 08:18
-
Far 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) – userfuser Jul 09 '15 at 08:12