Fatal error: Out of memory (allocated 1134559232)

2019-01-27 07:31发布

问题:

I am using WAMP server(32 bits) on local host on my personal PC. I have a big (very big) multidimensional array which gets its information by reading a CSV file which contains long sentences(the CSV file contains 20,000 row of information). The problem is that I get the following error when it goes through some calculations:

Fatal error: Out of memory (allocated 1134559232) (tried to allocate 32768 bytes) in x:\wamp\www\xxx

I tried different solutions like increasing upload_max_filesize, post_max_size , max_file_uploads and memory_limit or set it to -1 in php.ini or at the beginning of my scripts also, no one works. Please help me, and please do not ask me to re-architect my codes or change the version of WAMP, due to some reasons it is not possible. Thank you very much. :)

回答1:

Finally I could find the solution. I found that when the PHP collection garbage is getting full, there is no way to free it. Unset and gc_collect_cycles() also are not effective. The only way is to use Function over different section of codes. In my case, I had a big script in a for loop, so I copied all my codes in a function, and in my loop I call the function. Each time function quiets, memory gets free. You may test it by adding memory_get_usage() once in your function and once out of the function to see the difference.



回答2:

It's none of those settings, it is memory_limit, the max amount of memory that a PHP script can consume. However, be sure that your server has enough resources before arbitrarily increasing this setting.



回答3:

Put this line at the beginning of your code:

ini_set("memory_limit", -1);

The PHP manual gives the following description of memory_limit:

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.

Prior to PHP 5.2.1, in order to use this directive it had to be enabled at compile time by using --enable-memory-limit in the configure line. This compile-time flag was also required to define the functions memory_get_usage() and memory_get_peak_usage() prior to 5.2.1.

When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used.

I don’t know what makes them so confident that only poorly written scripts need to adjust this setting but I hope this brief introduction fulfils your need anyway.