Monday 31 December 2018

PHP - how to increase memory limit for scripts [ANSWERED]

Quick one - assuming you are looking to increase the memory of a php script, there are many ways, but the most effective in script option is to use the following command.


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

 ini_set('memory_limit','1G');



Which hopefully are self explanatory. After all, '1GB' is 'one GigaByte' and 'M' is MegaByte. Or in more modern parlance, 134217728 Bytes is 128 MebiByte.

Setting the php memory limit by putting the above line in your php code will allocate extra memory if needed up to the specified size. I usually increase the value until the message goes away.


The error message is usually 

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 54 bytes)

This is 128 Mebibyte - the default limit in PHP

There are many other resources on the web which suggest setting the value to -1 but this means that your server will allocate an unlimited amount of memory. Never good to do this because if you script is using up all the resources, other processes might get frozen or locked out. If your script is running on a web server, this may give the impression that your website is down.

So, adjust in small increments.