PHP: Fatal error: Allowed memory size of xx bytes exhausted (tried to allocate xx bytes)


When you see the following error message it means that the PHP script is exceeding the memory limit which is set for PHP.

Fatal error: Allowed memory size of xx bytes exhausted (tried to allocate xx bytes) in /path/to/script.php

To change the memory limit for one specific script, add the following line at the top of the script:

ini_set(“memory_limit”,”64M”);

You can also change the memory limit server-wide for all scripts.

1. Locate the PHP configuration file

# php -i | grep ini
Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini
Scan this dir for additional .ini files => (none)
additional .ini files parsed => (none)

As you see the php.ini is being used which is in /usr/local/lib

2. Open the configuration file

# nano -w /usr/local/lib/php.ini

3. Search for memory_limit and raise the limit, it should look like:

memory_limit = 64M

4. Save and exit.

5. Restart Apache

# /etc/init.d/apache2 restart
or
# /etc/init.d/httpd restart