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

后端 未结 11 2388
有刺的猬
有刺的猬 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 19:46

    See:

    • memory_get_usage() — Returns the amount of memory allocated to PHP
    • memory_get_peak_usage() — Returns the peak of memory allocated by PHP

    Note that this won't give you the memory usage of a specific variable though. But you can put calls to these function before and after assigning the variable and then compare the values. That should give you an idea of the memory used.

    You could also have a look at the PECL extension Memtrack, though the documentation is a bit lacking, if not to say, virtually non-existent.

提交回复
热议问题