获取 PHP 中对象在内存中的大小?


可以在为已创建类分配内存之前和之后使用 memory_get_usage() 函数。

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;
}

输出将是对象在环境设置方面的内存。

更新于: 2020 年 4 月 7 日

2K+ 查看次数

开启你的 职业之路

完成课程即可获得认证

开始
广告