Example of memory_get_usage getting the current PHP memory consumption

WBOY
Release: 2016-07-25 08:56:37
Original
1040 people have browsed it
? & Lt;? Php
echo Memory_get_usage ();
$ var = strikeat ("phpzixue.cn", 10000);
    echo memory_ut_usage (); y_get_usage ();
  1. ?>
  2. Copy code
  3. Output: 62328 122504 62416 Note: The value output by the memory_get_usage() function is in bytes
2, format memory_get_usage() output

function convert($size){

$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
    echo convert(memory_get_usage(true ));
  1. ?>
  2. Copy code
  3. Output: 256 kb
  4. 3. Custom function to obtain the size of array or variable value

    function array_size($arr) {

    ob_start();
    print_r($arr);
    $mem = ob_get_contents();
      ob_end_clean();
    1. $mem = preg_replace(" /n +/", "", $mem);
    2. $mem = strlen($mem);
    3. return $mem;
    4. }
    5. $memEstimate = array_size($GLOBALS);
    6. ?>
    7. Copy code
    8. Instructions: To reduce memory usage, you can use the PHP unset() function to delete variables that are no longer needed. Similar functions: PHP mysql_free_result() function can clear the result set obtained by querying the database that is no longer needed, so that more available memory can be obtained.
    9. PHP memory_get_usage() can also have a parameter, $real_usage, whose value is a Boolean value. The default is FALSE, which means that the obtained memory usage does not include the memory occupied by this function (PHP memory manager); When set to TRUE, the resulting memory includes the memory occupied by this function (PHP memory manager). In PHP programming, you can use PHP memory_get_usage() to compare the memory occupied by each method, and then choose the method with better performance.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template