Sometimes, when executing a script with a strong memory usage, we might reach the PHP memory limit.
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
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.
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:
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.
If you have set the memory limit in php.ini, you will need to restart apache, type/copy the following in a terminal:
sudo service apache2 restart