How to Fix PHP Out of Memory Errors

If you have used enough PHP scripts on web servers, you have undoubtedly encountered “out of memory” errors at some point.  While they are annoying and even damaging to business when they interrupt service on a website, they are relatively easy to diagnose and fix.

If you do a quick search for a solution to this problem, most will tell you to increase the memory allotment within php.ini.  To do this, find the php.ini file (usually in /etc/php.ini), and then locate the following line within it:

memory_limit = 16M

Then, increase the memory limit to whatever you need, 32M, 64M, or even more.

memory_limit = 64M

This may fix your problem, but it also may just serve as a workaround to a larger problem.  If the “out of memory” error appears suddenly, you may have a memory leak in your web application that you should address.  Some web applications are always going to require a lot of memory, but you may be able to increase efficiency by using caching and other tools to limit the amount of physical memory your server has to spend on a single process.

If the problem is a local issue with only one website, you might want to keep the global memory limit low (in /etc/php.ini) and only edit the php.ini file for the virtual host.  The location will vary according to how your virtual hosts are organized within your operating system and/or control panel (some control panels have their own directory structure for their web servers). Be sure to monitor your web applications after you increase the memory limit to make sure they handle it well.