Fatal error: Out of memory PHP

You may have come across this a few times during your development career, and in most cases it’s easy to either extend PHP’s memory or simply optimize the script you have written to use less memory, however sometimes you may need to extend PHP’s memory but the obvious functions you may be familiar to may not work on some configurations, this Knowledge Base article is aimed to troubleshoot the issues when you cannot extend PHP’s memory limit due to some safeguard process on the server itself. We will first start with the most obvious and common methods which work on most servers, as you go down the guide we will get to more advanced methods which will require SSH access with full root permissions, if you have a server administrator it will be best to contact them about your issue and maybe forward them this article for reference.  

Using ini_set to change php memory limit

So the most popular way to change PHP’s memory limit is within your PHP script itself, try adding the following line to the top of your PHP script, if your PHP out of memory error goes away then your problem is resolved.

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

If this does not work for you please move to the next method.  

Using htaccess to change php memory limit

Some servers allow you to override the php.ini in your .htaccess file, this is extremely useful and it means memory extending can be separated from the scripts, try the following value at the top of your root .htaccess file, if your PHP memory error message disappears then your problem is resolved!

php_value memory_limit 200M


Modify php.ini to increase memory limit

If you have gotten this far you are now in the realm of server administration, you will need SSH access to your server with full root access, or full root FTP access. The php.ini file can be located in several different places on a server depending on it’s configuration, the best thing to do is to ask PHP where it’s configuration file is if you have SSH access run this command:

php --ini

Take a look at Loaded Configuration File, this will be the path to your php.ini file, now open this file for editing using your favourite editor. Mine is Nano:

nano /usr/local/lib/php.ini

Find the line where PHP’s memory limit is set, search for memory_limit. Change the value to 200M. If this fixes your problem you are good to go!  

Nothing Above Working?

If you have tried all of the above and you are still not able to extend PHP’s memory limit then the server probably has some sort of safeguard in place to stop a script using the memory you need, this is great for security so please only continue if you are sure your server is not being used by third parties who could consume all the RAM and crash the server.  


Check RLimitMEM is not restricting your apache process, this is set in your apache configuration file, which should be at the below location:


Find the rlimit lines and comment them out, restart apache and see if your problem is resolved.  


Mod_Security is another security feature of most modern servers, it secures allot of features of apache and php so I would never recommend disabling it or all of its settings, fortunately though you can disable specific settings per directory per account using the htaccess, add the below commands to the top of your htaccess file:

<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off

Author: Dean Williams

Professional PHP Web Developer with expertise in OpenCart Web Development, WordPress Web Development, Bespoke Systems - also a seasoned Linux Server Administrator.

Leave a Reply

Your email address will not be published. Required fields are marked *