How to obtain PHP memory usage? php memory acquisition _ PHP Tutorial

Source: Internet
Author: User
How to obtain PHP memory usage and php memory usage. How to obtain the PHP memory usage? the php memory gets the memory size allocated to the PHP script by the built-in function memory_get_usage (), in bytes ). In actual WEB development, how can I obtain PHP memory usage and php memory usage?

The built-in PHP function memory_get_usage () can return the memory size currently allocated to the PHP script, in bytes ). In actual WEB development, these functions are very useful. 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 memory usage peak, and the getrusage () function returns the CPU usage. Note that these functions must be run on Linux.
Let's look at an example:

Echo 'start memory :'. memory_get_usage (), ''; $ tmp = str_repeat ('hello', 1000); echo 'memory after running :'. memory_get_usage (), ''; unset ($ tmp); echo 'back to normal memory :'. memory_get_usage ();

Output result:
Memory start: 147296
Memory after running: 152456
Back to normal memory: 147296
In this example, we use str_repeat () to repeat the string "hello" for 1000 times, and finally compare the memory size consumed before and after. The preceding example shows that to reduce memory usage, you can use the unset () function to delete unnecessary variables. Similarly, the mysql_free_result () function can be used to release the memory occupied by the query when we no longer need to query the result set of the data.
The memory_get_usage () function also has a parameter $ real_usage with a Boolean value. If this parameter is set to TRUE, the real memory size allocated by the system is obtained. If this parameter is not set or set to FALSE, the memory usage reported by emalloc () is used.
In actual WEB development, you can use PHP memory_get_usage () to compare the memory usage of each method and select the method that occupies a small amount of memory.
The number of bytes returned by the function memory_get_usage (), in bytes (s )).

The following custom function converts the number of bytes to MB, which is easier to read:

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

Common debugging methods for detecting PHP code performance include:
Memory_get_usage can be used to analyze memory usage.
You can use the microtime function to analyze the program execution time.

I hope this article will help you learn how php gets the memory usage.

The built-in function memory_get_usage () in PHP can return the memory size currently allocated to the PHP script, in bytes ). In actual WEB development...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.