views:

1654

answers:

3

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?

A: 

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

  1. Counts the ints
  2. Multiplies number of ints by size of an int on hard disk
  3. Convert characters in strings to ASCII and
  4. 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.

Peter
+7  A: 

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.

zombat
+1  A: 

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.

rekowski