Fatal error: Allowed memory size of 8388608 bytes exhausted

PHP Fatal error: Allowed memory size of X bytes exhausted (tried to allocate Y) in file.php

1. memory_limit globally from php.ini
memory_limit = 64M

2. memory_limit using .htaccess
php_value memory_limit 64M

3. memory_limit inside a php script.
ini_set(‘memory_limit’, ’64M’);

To change the memory limit for one specific script by including a line such as this at the top of the script:


The 12M sets the limit to 64 megabytes. If this doesn’t work, keep increasing the memory limit until your script fits.

You can change permanently for all PHP scripts running on the server by adding a line to the server’s php.ini file:

memory_limit = 64M

Keep in mind that a huge memory limit is a poor substitute for good coding. Some applications like data scrappers are run infrequently and require lots of memory like importing and processing a big data file using RSS, XML, JSON API etc.

Make sure each time script required server memory resources; need to release them when script/process finish first iteration to save memory uses 😉

PHP function might help….

1. unset — Unset a given variable

2. memory_get_usage — Returns the amount of memory allocated to PHP

3. memory_get_peak_usage — Returns the peak of memory allocated by PHP

4. __destruct your object references upon disposing of an object

5. The Alternative PHP Cache (APC) is a free and open opcode cache for PHP. Its goal is to provide a free, open, and robust framework for caching and optimizing PHP intermediate code.

6. Turn PHP scripts into Linux daemons

7. Xdebug’s Profiler is a powerful tool that gives you the ability to analyze your PHP code and determine bottlenecks or generally see which parts of your code are slow and could use a speed boost.

class Memory
public $var='3.1415962654';

$baseMemory = memory_get_usage();

$a=new Memory;
echo sprintf('%8d: ',$i), memory_get_usage() - $baseMemory,"\n";

Fatal error: Allowed memory size of 8388608 bytes exhausted


Quality is not a product.. it is a process