PHP uses the built-in function memory_get_usage () to obtain memory usage _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP uses the built-in function memory_get_usage () to obtain memory usage. PHP uses the built-in function memory_get_usage () to obtain memory usage. The PHP built-in function memory_get_usage () can return the memory size currently allocated to the PHP script, in bytes ). Use the built-in function memory_get_usage () to obtain the memory usage in PHP on the WEB.

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:

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 'back to normal memory: '. memory_get_usage ();

Output result:

The code is as follows:


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:

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;
}

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.

Memory () gets memory usage. The PHP built-in function memory_get_usage () returns the memory size currently allocated to the PHP script, in bytes ). In the WEB instance...

Related Article

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.