In-depth explanation of PHP memory-related features

Source: Internet
Author: User

Some readers may have encountered the following errors:
Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y bytes) This error message is clear, PHP has reached the maximum memory Allowed, generally, this may be a problem in programming. For example, if a large file is read to the memory at a time, an array is large, or a variable that is not used in a large loop is not released in time, these may cause excessive memory usage and termination.

The default maximum memory size of PHP is 32 MB. If you need to use a memory larger than 32 MB, you can modify the following configuration in the php. ini configuration file:

Memory_limit = 32 m if you cannot modify the php configuration file and the ini_set () function is not disabled in your PHP environment, you can also dynamically modify the maximum memory usage:

<? Php ini_set ("memory_limit", "128 M"); since we can dynamically adjust the maximum memory usage, is there a way to obtain the current memory usage? The answer is yes.

1. memory_get_usage ()This function is used to obtain the memory size currently used by the PHP script.
2. memory_get_peak_usage (), The function returns the memory peak occupied by the current script to the current position, so that the current script memory requirements may be obtained.
For the functions provided by PHP user space, we seem to be unable to control memory usage and can only passively obtain memory usage

So when we know that php reference counting, function tables, symbol tables, constant tables, and so on, this information will occupy the memory

We can intentionally avoid unnecessary waste of memory. For example, we usually use autoload in projects to avoid one-time inclusion of classes that may not be used, and this information will occupy the memory.

If we unset a variable that is no longer used in time, the space occupied by the variable may be released.

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.