PHP Fatal error: Allowed memory size of 2388608 bytes
I get this,开发者_运维技巧 when I try to run my PHP. When I comment out the database execute() method it is going without errors. But this is not helping me a lot. Please Help :-)
Generally you should optimize your query/execute(), for example do not use SELECT * FROM or any ORM can make memory problems too.
Workaround set the memory size higher:
ini_set("memory_limit","5M");
The 5M sets the limit to 5 megabytes. If this doesn’t work, try to set the value in php.ini.
Cheers.
Are you using "SELECT *" in the query? If yes, and you are fetching a TON of rows, PHP doesn't have enough memory to allocate to all fields.
You can increase PHP's memory limit by:
ini_set('memory_limit', '32M');
Without more specifics it's difficult to be sure what your specific problem is; however, this is common when trying to read in the results of a query with many records.
PHP has a memory_size_limit (set in php.ini) which sets a maximum on the amount of memory PHP can use to handle your script. If you try and read in a large number of records, and say, store it in an array, PHP is likely to run out of memory. One thing you can do is increase the memory size limit, say from 2 megabytes (2M) to 32M.
As others have mentioned, you can also use ini_set('memory_limit', '32M')
or similar, if you have access to do so on your host.
But regardless of whether you are allowed to edit your php.ini file, you really should look for a more efficient way to retrieve and store your data. Consider simplifying your query, limiting your query results, or just using something like while ($row = $db->fetch_row)
to deal with one result row at a time.
If you are using PHP 5.3 with mysqlnd, be aware that "MySQL's" memory usage now counts as PHP's memory usage. Before, memory used by libmysql would not be counted in memory_get_usage()
.
We changed a lot of our heavy queries to use unbuffered queries, which keeps memory usage down.
精彩评论