View memory usage of PHP executed on the page

Source: Internet
Author: User

Today, my mind is hot. I want to see how much memory the page occupies during execution. I don't know what the purpose is. Maybe it is out of my inertial thinking. However, this is not completely useless,
You can clearly know which pages occupy a large amount of memory, especially for websites that use shared hosts. Generally, space providers limit the resource usage (including CPU and memory) of each website ). In case of me
If you do not know, it often occupies too much memory. You are willing to, but the space provider does not agree. Of course, this situation rarely occurs.

Memory_get_usage

PHP provides the memory_get_usage function to view the allocated memory size. The prototype is as follows:

Int memory_get_usage ([bool $ real_usage = false])

$ Real_usage is an optional parameter. The default value is false (excluding the memory occupied by memory_get_usage). memory_get_usage (true) can be used to view memory usage including memory_get_usage.

By default, this function returns the number of bytes in memory. We can divide the number by 1024 to kb, and then divide the number by 1024 in MB.

Usage

For WordPress, you only need to call the memory_get_usage function in the footer. php file in the topic folder.

<? PHP printf ('memory usage: % 01.2f MB ', memory_get_usage ()/1024/1024);?>

The output of this statement is in MB, with two digits after the decimal point. For more information, see the bottom of this site.

Digress

Of course, if you want to perform a detailed test, you canCodeBefore Execution, before the template call, and after the Page code is executed, call the function for comparison. The following is my test:

1. Call memory_get_usage before executing the Page code, that is, add the above Code to the first part of the index. php file in the root directory, and the result is about M.

2. Before the template is called, memory_get_usage is called at the very beginning of the header. php file in the topic folder. The result is around 11 Mb. This indicates that WordPress has loaded a large amount of data before calling the template.

3. After the Page code is executed, the last part of the footer. php file in the topic folder calls memory_get_usage.
Same, but it is almost below 12 m. It indicates that the PHP call of the topic does not occupy much memory. If this result is very high, check your topic file.

The above results vary depending on the host, WordPress, theme, and various factors ...... For reference only.

In addition, if you want to test the memory usage after code execution, you can call memory_get_usage before and after code execution to see the impact of that code on the memory.

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.