When running dedecms, some friends will find that some pages will prompt the Fatal error: Allowed memory size of 134217728 bytes exhauste error. Let's take a look at the solution to this problem.
Error message: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 38218371 bytes) in .....
Solution:
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.
Experience
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.