PHP memory management function memory_get_usage () usage introduction _ PHP

Source: Internet
Author: User
In actual encoding, to view and operate the memory, many programmers first think of the PHP script memory function PHPmemory_get_usage (). below is the PHP memory_get_usage () Usage example:
The code is as follows:
Echo memory_get_usage (),'
'; // 143952
$ Tmp = str_repeat ('http: // blog.micxp.com/', 4000 );
Echo memory_get_usage (),'
'; // 232048
Unset ($ tmp );
Echo memory_get_usage (); // 143952

The comments behind the above program represent their output (unit: byte (s), that is, the memory used by the PHP script at that time (excluding the memory occupied by the memory_get_usage () function itself)

The preceding example shows that to reduce memory usage, you can use the PHP unset () function to delete unnecessary variables. Similarly, the PHP mysql_free_result () function can be used to clear the result set of a database query that is no longer needed, so that more available memory can be obtained.

PHP memory_get_usage () can also have a parameter $ real_usage with a Boolean value. The default value is FALSE, indicating that the obtained memory usage does not include the memory occupied by this function (PHP memory manager). when it is set to TRUE, the obtained memory includes this function (PHP memory manager) memory used.

Therefore, in actual programming, 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.

Common detection:

You can use the microtime function to analyze the program execution time.
Memory_get_usage can analyze memory usage
SQL efficiency can be used to open slow queries to view log analysis
Use EXPLAIN to analyze SQL statements with bottlenecks

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.