PHP memory limit - increase memory allocation

Sometimes, when executing a script with a strong memory usage, we might reach the PHP memory limit.

Fatal Error

This is what we can see when that moment happens:

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

Configuring php.ini

This means you have exceeded PHP memory limit. The default value is set in php.ini file with the following line:

memory_limit “amount of memory

To change this you need to open your php.ini file (which can be located in a number of places depending on your system: find where is php.ini) and edit this line.

Example:

memory_limit "512M"

We will now need to restart Apache to allow your modification to take effect.

Setting PHP memory limit with ini_set()

In some cases we will not be able, or do not want to edit our php.ini file. We can also set the limit in a script, with the ini_set function:

ini_set('memory_limit','512M');

NOTE: When dealing with time-consuming operations, PHP might time out when it reaches the time limit. However, other than PHP memory limit, we can also set PHP time limit.

 

Another Solution via .htaccess

Maybe another solution would be with a .htaccess file with something like:

php_value memory_limit 36M
php_value post_max_size 36M
php_value upload_max_filesize 32M

 

Visit sunny St. George, Utah, USA