Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "Allowed memory size exhausted"?

Tags:

php

memory

I am writing a batch script and get a Allowed memory size of 134217728 bytes exhausted error.

I don't understand why the memory is filling up. I tried unsetting the $row variable, but that didn't change a thing. Here is my code:

// ... (sql connection)
$result = mysql_query("SELECT * FROM large_table");

while ($row = mysql_fetch_array($result)) {
    echo $row['id'] . PHP_EOL;
    unset($row);
}

(simplified code)

Why does the memory fill up, and how can I avoid it?

Note: this is a batch script. This is normal that I have to handle data like that (go through 1 million lines).

Update: The out of memory happens around the 400 000th line, so this has got to be something in the loop? I'd like to avoid having to implement the paging if possible.

like image 316
Matthieu Napoli Avatar asked Feb 11 '12 18:02

Matthieu Napoli


People also ask

How do I fix memory size exhausted?

You can find the php. ini file in the public_html for your website and right-click on the file to Edit it. Look for the line defining the memory_limit variable and set the value accordingly. Then, save the changes and reload your site to see if the PHP “Allowed Memory Size of Bytes Exhausted” error has been resolved.

How do I fix the allowed memory size 134217728 bytes exhausted?

You can adjust the memory allocation on the fly from within a PHP file. You simply have the code ini_set('memory_limit', '128M'); (or whatever your desired allocation is). You can remove the memory limit (although machine or instance limits may still apply) by setting the value to "-1".

How do I fix the allowed memory size 1610612736 bytes exhausted?

Allowed memory size of 1610612736 bytes exhausted ??? can someone help me this? Try prefixing your command with COMPOSER_MEMORY_LIMIT=-1 . This will remove the memory limit for the execution of the command.


1 Answers

Try using http://www.php.net/manual/en/function.mysql-unbuffered-query.php (mysql_unbuffered_query()) to prevent the whole table being loaded into memory, but still avoiding pagination.

like image 141
KillerX Avatar answered Oct 03 '22 14:10

KillerX