Getting size in memory of an object in PHP?


The memory_get_usage() function can be caked before and after allocating memory to the class created.

class MyBigClass {
   var $allocatedSize;
   var $allMyOtherStuff;
}
function AllocateMyBigClass() {
   $before = memory_get_usage();
   $ret = new MyBigClass;
   $after = memory_get_usage();
   $ret->allocatedSize = ($after - $before);
   return $ret;
}

Output will be the memory of object with respect to the environment setup.

Updated on: 07-Apr-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements