? & Lt;? Php echo Memory_get_usage (); $ var = strikeat ("phpzixue.cn", 10000); echo memory_ut_usage (); y_get_usage (); - ?>
-
-
-
- Copy code
-
-
- 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 )); - ?>
-
-
-
- Copy code
-
-
- Output: 256 kb
-
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(); - $mem = preg_replace(" /n +/", "", $mem);
- $mem = strlen($mem);
- return $mem;
- }
- $memEstimate = array_size($GLOBALS);
- ?>
-
-
- Copy code
-
-
- 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.
-
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.
|