PHP Fatal error Out of memory

大城市里の小女人 提交于 2019-12-01 13:29:39
Ray

1517289472 bytes ~= 1.4G so you're hitting a barrier around there

The first thing you should look at is you're setting memory_limit: 20000M which is 20G. You've only 6G on your system. PHP may be confused and defaulting to a lower limit or there may be a hard limit for your version of php running on windows 7.

OR you could be running 32 bit Apache/PHP processes, which have hard limits around 2G see this: Upper memory limit for PHP/Apache

Populus

if you're going through a tonne of data, do not use fetch_all() which is what I'm assuming you are using within your database class.

you should fetch each record individually, e.g.:

while ($row = mysqli_fetch_array($link)) {
    // insert row into file
}

here is a simple benchmark someone did before to illustrate the difference in memory usage: https://stackoverflow.com/a/2771563/520857

spoiler: 100Mb vs 400bytes

Doesn't matter how much ram you allow for PHP. If it's a 32bit-compile, it could never use more than 4gig utter max, and practical ~3gig.

As for your code, you're sucking in your ENTIRE database result into ram, then building a string (essentially doubling memory requirements), then dumping that string out to file. Since you're obviously running out of memory, you'll have to stream the string, e.g.

$fh = fopen(...);
$separator = '';
while($row = fetch_from_db()) {
     fwrite($fh, $sep . $row[0]);
     $separator = '|';
}

This is somewhat inefficient, but should be enough to give you the general idea. A more efficient solution would be to build a LIMITED length string and write it out periodically. e.g. when your concatentated string reaches (say) 1 meg in size, you write it out and then reset the string to empty.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!