PHP memory usage How to get, PHP memory get _php Tutorial

Source: Internet
Author: User

How to get PHP memory usage, PHP memory acquisition

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:

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:

function Memory_usage () {  $memory  = (! function_exists (' memory_get_usage '))? ' 0 ': Round (Memory_get_usage ()/1024/1024, 2). ' MB ';  

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.

Through this article you know how to get the memory usage of PHP, it is hoped that this article can be helpful to everyone's study. true techarticle PHP Memory Usage How to get, PHP memory get PHP Built-in function memory_get_usage () can return the current amount allocated to the php script, in bytes (byte). In the actual development of the Web ...

  • 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: 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.