How to determine the memory footprint (size) of a variable?

后端 未结 11 2387
有刺的猬
有刺的猬 2020-11-28 19:30

Is there a function in PHP (or a PHP extension) to find out how much memory a given variable uses? sizeof just tells me the number of elements/properties.

11条回答
  •  攒了一身酷
    2020-11-28 20:08

    function mesure($var){
        $start = memory_get_usage();
        if(is_string($var)){
            $newValue = $var . '';
        }elseif(is_numeric($var)){
            $newValue = $var + 0;
        }elseif(is_object($var)){
            $newValue = clone $var;
        }elseif(is_array($var)){
            $newValue = array_flip($var, []);
        }
        return memory_get_usage() - $start;
    }
    

提交回复
热议问题