Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

My project uses an open source PHP MySQL library https://github.com/ajillion/PHP-MySQLi-Database-Class

But the project mid-year report: "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 4294967296 bytes) in / home1/flipalbu/public_html/kvisofttest/login-admin/Lib/class.MysqliDb.php on line 422" This error ,

My server is: linux x86_64

PHP Version 5.4.17

Mysql Version: 5.5.32

memory_limit = 128M

Line 422:call_user_func_array (array ($ stmt, 'bind_result'), $ parameters);

Query part of the code:

    $ db = new MysqliDb ('LocalHost', 'root', 'PASSWD', 'DB');
$ wqdb = $ db-> query ("SELECT * FROM db_table");
foreach ($ wqdb as $ row) {
     $ con. = $ row ['ID'];
}
echo $ con;

Is there any way to solve it?


/** Error Code **/

 protected function _dynamicBindResults(mysqli_stmt $stmt)
        {
            $parameters = array();
            $results = array();

            $meta = $stmt->result_metadata();

            $row = array();
            while ($field = $meta->fetch_field()) {
                $row[$field->name] = null;
                $parameters[] = & $row[$field->name];
            }

            call_user_func_array(array($stmt, 'bind_result'), $parameters);

            while ($stmt->fetch()) {
                $x = array();
                foreach ($row as $key => $val) {
                    $x[$key] = $val;
                }
                array_push($results, $x);
            }
            return $results;
        }
like image 741
user2637147 Avatar asked Aug 08 '13 08:08

user2637147


People also ask

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

The correct way is to edit your php. ini file. Edit memory_limit to your desire value. As from your question, 128M (which is the default limit) has been exceeded, so there is something seriously wrong with your code as it should not take that much.

How do I fix memory size exhausted?

The memory_limit setting in your server's PHP configuration controls this. To check the current memory limit setting, go to Utilities > System > PHP Info. Find the memory_limit directive and note the local value column's value. Increase this setting until the error is resolved.

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.

How do I fix PHP fatal error allowed memory size?

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.


1 Answers

I read this bug report here: https://bugs.php.net/bug.php?id=51386

Your problem seems to happen because there is a longblob or longtext in the columns of the table.

longtext / longblob have a maximum length of 4294967295 [4GB] thats why mysqli tries to allocated that memory for the buffer to be sure nothing is lost. I would suggest that you use mediumtext (16777215 [16MB] max length), that should be enough for everything usually.

Update: Because this answer has seen some activity I add this solution from Phil_1984 (see comments)

I use mysqli and after reading that quote from php dev, adding a $stmt->store_result(); between execute and bind_result seems to fix the issues for me

=> If you use $stmt->store_result() you can use mysqli with longblob / longtext without getting the error.

-

Old Answer: I suggest that you either change the column to another type (mediumtext) or use PDO (i think it doesnt have that problem). but if you want to keep the column as longtext, you have to switch your mysql library

Quote from PHP Dev:

This is a known limitation of ext/mysqli when using libmysql (always in 5.2 and previous) and when libmysql is enabled with 5.3 . The reason is that the server sends not too specific metadata about the column. This longtext has a max length of 4G and ext/mysqli tries to bind with the max length, to be sure no data loss occurs (data doesn't fit in the bind buffer on C level). However, that means 4G for a longtext/longblob column. ext/mysqli has been changed to have a way to work around that. You need to call mysqli_stmt_store_result() which will store the data locally, which means, of course a higher memory usage for PHP. However, because you use libmysql this won't hit the PHP's memory limit, for sure. During store_result the max_length of every column will be calculated and then when bind_result is executed only a buffer with size of max_length will be allocated, which will be definitely lower than 4G. In short, prepare execute store_result bind_result fetch...fetch...fetch

like image 54
x4rf41 Avatar answered Oct 13 '22 00:10

x4rf41