Home  >  Article  >  Backend Development  >  PHP obtains memory usage through the built-in function memory_get_usage(), memorygetusage_PHP tutorial

PHP obtains memory usage through the built-in function memory_get_usage(), memorygetusage_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:26910browse

PHP obtains memory usage through the built-in function memory_get_usage(), memorygetusage

PHP built-in function memory_get_usage() can return the amount of memory currently allocated to the PHP script, in bytes. In actual WEB development, these functions are very useful, and we can use them to debug PHP code performance.
The memory_get_usage() function returns the memory usage, the memory_get_peak_usage() function returns the peak memory usage, and getrusage() returns the CUP usage. But please note that these functions need to run on Linux.
Let’s take a look at an example:

Copy code The code is as follows:

echo 'Start memory:'.memory_get_usage(), '';
$tmp = str_repeat('hello', 1000);
echo 'Memory after running:'.memory_get_usage(), '';
unset($tmp);
echo 'Return to normal memory:'.memory_get_usage();

Output result:

Copy code The code is as follows:

Starting memory: 147296
Memory after running: 152456
Back to normal memory: 147296

In the example, we use str_repeat() to repeat the string "hello" 1000 times, and finally compare the memory consumed before and after. As can be seen from the above example, in order to reduce memory usage, you can use the unset() function to delete variables that are no longer needed. Similar to the mysql_free_result() function, when we no longer need the result set obtained by querying data, we can use it to release the memory occupied by the query.
The function memory_get_usage() can also have a parameter, $real_usage, whose value is a Boolean value. If set to TRUE, obtains the actual memory size allocated by the system. If unset or set to FALSE, emalloc() will report the amount of memory used.
In actual WEB development, you can use PHP memory_get_usage() to compare the memory occupied by each method to choose which method takes up less memory.
The number of bytes returned by the function memory_get_usage() (unit: byte(s)). Here’s a custom function that converts bytes to MB for easier reading:

Copy code The code is as follows:

function memory_usage() {
$memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
Return $memory;
}

Commonly used methods for debugging and testing PHP code performance are:
memory_get_usage can analyze the memory footprint.
You can use the microtime function to analyze program execution time.

The above is the entire content of using memory_get_usage to obtain the memory occupied by the PHP code. This function is very efficient. Friends in need can refer to it

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/915436.htmlTechArticlePHP obtains the memory usage through the built-in function memory_get_usage(), memorygetusage The PHP built-in function memory_get_usage() can return the current allocation to The amount of memory of the PHP script, in bytes (...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn