The memory_get_usage function is provided for you in php. You can check the current memory usage on your server. With this function, you can check the server status in real time. Next I will introduce the usage of memory_get_usage.
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;
}
}
} |