Getting size in memory of an object in PHP?

前端 未结 5 1875
旧时难觅i
旧时难觅i 2020-12-09 08:09

There is a way to get the total memory PHP is using (memory_get_usage()) but how does one get the size in memory of an individual object?

I\'m obviously

5条回答
  •  借酒劲吻你
    2020-12-09 08:26

    Since clone (from Prof83's answer) didn't work for me, I tried to serialize and unserialize the variable whose size I want to measure:

    function getMemoryUsage($var) {
        $mem = memory_get_usage();
        $tmp = unserialize(serialize($var));
        // Return the unserialized memory usage
        return memory_get_usage() - $mem;
    }
    

    I think it reports better results, at least for me.

提交回复
热议问题