Fatal error: Allowed memory size of 134217728 bytes exhauste

Source: Internet
Author: User
Tags memory usage

Error message: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 38218371 bytes) in .....


1. Cancel the PHP memory limit.
Add ini_set ("memory_limit", "-1") in the php program ");
2. Modify the php memory limit according to your own needs and the local memory size. For example, change it to 1024 MB.
Add ini_set ("memory_limit", "1024 M") to the php program, or change the corresponding position in php. ini to memory_limit = 1024 M;

Significance of memory limit

The relevant documents in php explain memory_limit as follows:

Memory_limit: integer

This command sets the maximum number of memory bytes that a script can apply. This helps prevent poorly written scripts from consuming the available memory on the light server. To use this command, it must be activated during compilation. Therefore, the configure row should include: -- enable-memory-limit. If you do not need any memory restrictions, you must set it to-1. From php 4.3.2, when memory_limit is activated, the PHP function memory_get_usage () can be used. That is to say, the processing memory limit of php in a page is default (/etc/php. ini) is defined as 128 M (my system is installed by default). Later, the development group's application writing becomes more and more complex, but the structure may still be somewhat lacking, frequent object requests, the memory is insufficient.

Test and solution at the application level

The best method should be solved at the application level, rather than constantly adding memory settings. Run the following code for testing:

The code is as follows: Copy code

Printf ("total run: %. 2f s <br> ".
"Memory usage: %. 2f M <br> ",
Microtime (true)-$ HeaderTime,
Apsaradb for memory_get_usage ()/1024/1024 );

The running result is as follows:

Total runtime: 1.47 s

Memory usage: 77.09 M

A page has a MB request. The reason is that when coding, programmers only assign values to variables, but never unset ($ var. Imagine if a page request needs to process 20 SQL queries, and each SQL query returns 10 SQL results, the programmer never cares whether to return all columns in a row or only the required columns (in fact, when we adopt a more common middle layer, usually, all columns are returned instead of some specific fields, just like in the ORM such as nhib.pdf and JBOSS.) if a row has 10 K, the page will be allocated with an array of 10 K * 10*20 = 2 m at the end of processing. This is not the case that we sometimes need to copy the most array.

Therefore, in php, the reasonable method is to unset ($ var) the variable after it is used to maximize memory resource savings.


I have a server that uses the methods above apaceh2.3 and php5.2.6, which are invalid. After official confirmation, it is a php version bug. We can change the php version to a higher version.

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.