PHP: Measure size in kilobytes of a object/array?

前端 未结 2 558
攒了一身酷
攒了一身酷 2020-12-29 06:01
  • What\'s an appropriate way of measure a PHP objects actual size in bytes/kilobytes?

Reason for asking:
I am utilizing memcached for cache storage in

相关标签:
2条回答
  • 2020-12-29 06:46

    Well, since Memcached doesn't store raw objects (it actually stores the serialiezd version), you can do this:

    $serializedFoo = serialize($foo);
    if (function_exists('mb_strlen')) {
        $size = mb_strlen($serializedFoo, '8bit');
    } else {
        $size = strlen($serializedFoo);
    }
    
    0 讨论(0)
  • 2020-12-29 06:49

    Another easy way pute content of array to file and then check file size.

    $str = print_r($array, true);
    file_put_contents('data.txt', $str);
    $size = filesize('data.txt');
    
    0 讨论(0)
提交回复
热议问题