PHP memory depletion error analysis-php Tutorial

Source: Internet
Author: User
PHP memory depletion error analysis someone recently told me that an error often occurs during the use of a wordpress plug-in on the host. after enabling the dispaly_error function of php, I found that it reported "Fatal error: allowed memory size of 35389440 bytes exhausted (tried to allocate 1406507 bytes) in xxxxxx on line xxx ", which means a fatal error and the memory is exhausted. After searching on Google, there are many such questions on the Internet and the answers are the same. change the memory limit of php and change memory_limit in php. ini to a larger value.

However, there is a big problem here. the size of the memory allowed by php to access is 35389440 bytes, but only 1406507 bytes are actually allocated. Why does the memory allocated cause a fatal error if it is smaller than the memory actually allowed for access?

I checked a lot of this information and found that many such search results are actually allocated with less memory than the allowed memory, but I did not find any description of this.

The first thought was whether PHP had a bug in the memory limit function. so I did the following experiment:

1. generate a 10 mb file dd if =/dev/zero of = 10 mb bs = 1 M count = 10

2. Generate and access the following php file:

 

The error "Fatal error: Allowed memory size of 2097152 bytes exhausted (tried to allocate 10493952 bytes)" is found. this error is consistent with the 2 MB memory limit we set, the actual allocation of 10 Mb is the same. This indicates that the memory limit function is correct, and the error data can be matched with the actual data.

So what is the problem above? Why does the memory actually need to be allocated be smaller than the limited memory? I also checked the description of memory_limit in php:

This sets the maximum amount of memory in bytes that a script is allowed to allocate. this helps prevent poorly written scripts for eating up all available memory on a server. note that to have no memory limit, set this directive to-1.

The above problem is not described here, but it only tells us that memory_limit is used to limit the memory size allocated in a script to protect the server from the impact of poorly written scripts. That is to say, memory_limit restricts the accessible memory size of a single PHP script. Is there a possibility that the memory of multiple operations in the entire thread has exceeded the total size, however, the memory size of a single operation only accounts for a small part of the entire limit. In the end, the straw that killed the camels was reported by PHP, so the attempt to access the memory smaller than the limit is displayed? To verify this conjecture, I changed the above php file to this and accessed:

 

The error "Fatal error: Allowed memory size of 15728640 bytes exhausted (tried to allocate 10493952 bytes)" Is Reported. the actual memory allocated is 10 MB, which is less than 15 MB of the access permitted, the above conjecture is verified. That is to say, if the program exceeds the configured memory during runtime, the PHP error message only prompts the size of the memory to be accessed by the current operation.

This problem can be solved by directly increasing the memory limit. you can register the memory_get_usage function on the shutdown function of php to calculate the memory consumption of the website, take a proper value.

References
  1. Memory limit

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.