php 致命错误:X:\wamp\www\xxx 中的内存不足(已分配 1134559232)(试图分配 32768 字节)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23050090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:27:13  来源:igfitidea点击:

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

phpmysqlphpmyadminwampserver

提问by Arash

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:

我在个人 PC 上的本地主机上使用WAMP 服务器(32 位)。我有一个大(非常大)多维数组,它通过读取包含长句子的 CSV 文件(CSV 文件包含 20,000 行信息)来获取其信息。问题是我在进行一些计算时收到以下错误:

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

致命错误:x:\wamp\www\xxx 中的内存不足(已分配 1134559232)(试图分配 32768 字节)

I tried different solutions like increasing upload_max_filesize, post_max_size, max_file_uploadsand memory_limitor 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. :)

我尝试了不同的解决方案,如增加upload_max_filesizepost_max_sizemax_file_uploadsmemory_limit或将其设置为-1在php.ini或在我的脚本的开始也没有人的作品。请帮助我,请不要让我重新构建我的代码或更改 WAMP 的版本,由于某些原因这是不可能的。非常感谢。:)

回答by Arash

Finally I could find the solution. I found that when the PHP collection garbage is getting full, there is no way to free it. Unsetand gc_collect_cycles()also are not effective. The only way is to use Functionover 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.

最后我找到了解决方案。我发现当PHP收集垃圾越来越满时,没有办法释放它。Unset而且gc_collect_cycles()也没有效果。唯一的方法是使用Function不同的代码段。就我而言,我在 for 循环中有一个大脚本,所以我将所有代码复制到一个函数中,并在我的循环中调用该函数。每次函数安静时,内存就会得到释放。您可以通过memory_get_usage()在函数中添加一次和从函数中添加一次来测试它以查看差异。

回答by robjingram

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.

这些设置都不是,而是memory_limitPHP 脚本可以消耗的最大内存量。但是,在随意增加此设置之前,请确保您的服务器有足够的资源。

回答by Sharanya Dutta

Put this line at the beginning of your code:

将此行放在代码的开头:

ini_set("memory_limit", -1);

The PHP manual gives the following description of memory_limit:

PHP手册给出了以下描述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 integeris used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used.

这设置了允许脚本分配的最大内存量(以字节为单位)。这有助于防止编写不当的脚本来耗尽服务器上的所有可用内存。请注意,要没有内存限制,请将此指令设置为 -1。

在 PHP 5.2.1 之前,为了使用这个指令,它必须在编译时通过在配置行中使用 --enable-memory-limit 来启用。在 5.2.1 之前,还需要此编译时标志来定义函数memory_get_usage()memory_get_peak_usage()

使用整数时,该值以字节为单位。也可以使用本常见问题解答中所述的速记符号。

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

我不知道是什么让他们如此自信,以至于只有写得不好的脚本才需要调整此设置,但我希望这个简短的介绍无论如何都能满足您的需求。