PHP Get Memory usage details

Source: Internet
Author: User
This article will take a detailed explanation of PHP for memory usage.

PHP built-in function memory_get_usage () can return the amount of memory currently allocated to a PHP script in bytes. These functions are very useful in web real-world development, and we can use it 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 one thing to note is that these functions need to be run on Linux.

Let's look at an example:

Echo ' Start memory: '. Memory_get_usage (), '; $tmp =str_repeat (' hello ', ' + '); Echo ' Run Memory: '. Memory_get_usage (), '; unset ($tmp) Echo ' Back to normal memory: '. Memory_get_usage ();

Output Result:

Start Memory: 147296

Memory after run: 152456

Back to normal memory: 147296

example, we use Str_repeat () to repeat the string "Hello" 1000 times, and finally to compare the memory consumed before and after the size. As you can see from the above example, to reduce the memory footprint, you can use the unset () function to remove variables that you no longer need to use. Similar to the Mysql_free_result () function, when we no longer need to query the resulting set of data, we can use the memory consumed by the free query.

The function memory_get_usage () can also have a parameter, $real _usage, whose value is a Boolean value. If set to true, gets the actual memory size allocated by the system. If not set or set to FALSE, the amount of memory used by the Emalloc () report is reported.

In real-world web development, you can use PHP memory_get_usage () to compare the amount of memory used by each method to choose the method that uses less memory.

The number of bytes (in bytes) returned by the function Memory_get_usage (). The following custom function converts the number of bytes into megabytes to be easier to read:

Functionmemory_usage () {$memory = (!function_exists (' memory_get_usage ')) ' 0 ': Round (Memory_get_usage ()/1024/1024,2) .' MB '; return$memory;}

Common debugging methods to detect the performance of PHP code are:

Memory_get_usage can analyze memory footprint.

You can use the Microtime function to analyze program execution time.

This article details the use of PHP to get memory, more relevant content please follow the PHP Chinese web.

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.