Format memory_get_usage () output
| The code is as follows: |
Copy code |
|
<? Php
Function convert ($ size ){
$ Unit = array ('B', 'KB', 'mb', 'GB', 'TB', 'petab ');
Return @ round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2 ). ''. $ unit [$ I];
}
Echo convert (memory_get_usage (true ));
?> |
Output: 256 kb
The PHP memory_get_usage () function can also have a parameter $ real_usage with a Boolean value. The default value is FALSE, indicating that the obtained memory usage does not include the memory occupied by this function (PHP memory manager). If it is set to TRUE, the resulting memory does not include the memory occupied by this function (PHP memory manager.
Example
| The code is as follows: |
Copy code |
|
If (! Function_exists ('memory _ get_usage '))
{
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;
}
}
} |