• 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 187

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 187
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

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?

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.

  • 49
Reply Report
    • Another 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... :)
      • 1
    • 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.
      • 1
    • Yeah? Well, what if PHP decides to play with the memory just before calling the second memory_get_usage?...very brittle solution....
    • This 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.
      • 2
    • This 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.

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.

  • 13
Reply Report

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.

  • 10
Reply Report

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.

  • -6
Reply Report
      • 1
    • Not 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....
      • 1
    • 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)