The following are examples of use:
Copy CodeThe code is as follows:
Echo memory_get_usage (), '
'; 313864
$tmp = str_repeat (' http://www.nowamagic.net/', 4000);
Echo memory_get_usage (), '
'; 406048
Unset ($TMP);
Echo Memory_get_usage (); 313952
?>
The comments that follow the above program represent their output in byte (s), which is the memory used by the PHP script (which does not contain the memory occupied by the Memory_get_usage () function itself).
As you can see from the above example, to reduce the memory footprint, you can use the PHP unset () function to remove variables that you no longer need to use. Similar to this: the PHP mysql_free_result () function, which empties the result set from the query database that is no longer needed, can also get more usable memory.
The PHP memory_get_usage () function 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 not included in the memory occupied by the function (PHP memory manager).
So in the actual programming, you can use the Memory_get_usage () function to compare the amount of memory used by each method, to choose the use of a small memory-consuming method.
Paste a usage function:
Copy CodeThe code is as follows:
if (!function_exists (' memory_get_usage '))
{
/**
+----------------------------------------------------------
* Get Memory usage
+----------------------------------------------------------
* @return Integer
+----------------------------------------------------------
*/
function Memory_get_usage ()
{
$pid = Getmypid ();
if (Is_win)
{
EXEC (' tasklist/fi ' PID eq '). $pid. '/fo LIST ', $output);
Return preg_replace ('/[^0-9]/', ', $output [5]) * 1024;
}
Else
{
EXEC ("Ps-eo%mem,rss,pid | grep $pid ", $output);
$output = Explode ("", $output [0]);
return $output [1] * 1024;
}
}
}
Here's another example of function use:
Copy CodeThe code is as follows:
Memory_get_usage ();
$m 1 = memory_get_usage ();
Echo '
M1: ', $m 1;//58096
$a = ' Hello ';
$b = Str_repeat ($a, 1000);
$m 2 = Memory_get_usage ();
Echo '
M2: ', $m 2;//63424
Unset ($b);
$m 3 = Memory_get_usage ();
Echo '
M3: ', $m 3;//58456
?>
http://www.bkjia.com/PHPjc/325945.html www.bkjia.com true http://www.bkjia.com/PHPjc/325945.html techarticle Here is the example of Use: Copy code code as follows:? PHP echo Memory_get_usage (), ' br/';//313864 $tmp = str_repeat (' http://www.nowamagic.net/', 400 0); Echo memory_get_usage (), ' B ...