php 允许的内存大小为 134217728 字节耗尽(尝试分配 42 字节)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36999406/
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-26 01:01:16  来源:igfitidea点击:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 42 bytes)

phpmysqlloops

提问by Abdul Rahman

I am retrieving record from mysql table which return more than 0.2m number of rows per query, which obviously take lot of memory. in my case i have 8 GBs installed RAM on my system with SSD 256 GBs. When i execute my page it returns the following error:

我正在从 mysql 表中检索记录,每个查询返回超过 0.2m 的行数,这显然占用了大量内存。就我而言,我的系统上安装了 8 GB 的 RAM,SSD 为 256 GB。当我执行我的页面时,它返回以下错误:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 42 bytes) in D:\xampp\htdocs\classes\CRUD.php on line 84   

I think i should need to use threading instead of php loops over table rows? Maybe i am wrong. Any suggestion/help will be appreciated.

我想我应该需要在表行上使用线程而不是 php 循环?也许我错了。任何建议/帮助将不胜感激。

回答by Maciej Asembler

Default php.ini memory_limit is 128 MB. You should either:

默认 php.ini memory_limit 为 128 MB。你应该:

  • Optimize your code to use a normal amount of data
  • change memory_limit in php.ini to higher value which I do not recommend at all - with your approach you will hit this wall once again
  • 优化您的代码以使用正常的数据量
  • 将 php.ini 中的 memory_limit 更改为更高的值,我根本不推荐 - 使用您的方法,您将再次撞到这堵墙

回答by Tom Cash

You can expand your memory from within your file with the following line of code:

您可以使用以下代码行从文件中扩展您的内存:

ini_set('memory_limit','16M');

Add this code to the top of your file and ahange 16M to be whatever you need it be and that should do it.

将此代码添加到文件的顶部,然后将 16M 设置为您需要的任何内容,并且应该这样做。