- Echo Memory_get_usage ();
- $var = Str_repeat ("phpzixue.cn", 10000);
- Echo Memory_get_usage ();
- Unset ($var);
- Echo Memory_get_usage ();
- ?>
Copy CodeOutput: 62328 122504 62416 Description: The Memory_get_usage () function outputs a value of bytes units 2, Format memory_get_usage () output
- function Convert ($size) {
- $unit =array (' B ', ' KB ', ' MB ', ' GB ', ' TB ', ' PB ');
- Return @round ($size/pow (1024x768, ($i =floor (log ($size, 1024))), 2). ' '. $unit [$i];
- }
- echo Convert (Memory_get_usage (true));
- ?>
Copy CodeOutput: KB 3, Custom function get array or variable value size
- 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 CodeNote: To reduce memory usage, you can use the PHP unset () function to remove variables that you no longer need to use. Similar functions: The PHP mysql_free_result () function, which empties the resulting set of query databases that are no longer needed, can also get more usable memory. PHP Memory_get_usage () can also have a parameter, $real _usage, whose value is a Boolean value. The default is FALSE, which indicates that the resulting memory usage does not include the memory used by the function (PHP memory manager), and when set to TRUE, the resulting memory is included in the memory used by the function (PHP memory manager). In PHP programming, you can use PHP memory_get_usage () to compare the various methods used to occupy memory, and then choose a better method. |