Have you ever gotten this Linux “out of memory” error when working with your WordPress site?
Fatal error: Out of memory (allocated 63963136) (tried to allocate 7864320 bytes) in [path]/wp-content/plugins/wordfence/lib/wfBrowscapCache.php.php on line 16388
If you Google around to solve this problem, almost all of the advice you’ll get is to increase your PHP and WordPress memory limits, or add memory to your server, or to remove plugins.
I didn’t want to just increase memory. I wanted to solve the root cause of the problem. The site was running on an Amazon EC2 small instance with Amazon AMI. It should not be running out of memory!
If you want to go directly to my results, jump ahead to my recommendations. Otherwise, here’s my story:
How I Investigated This Problem
When I first got this error, I installed the TPC! Memory Usage Plugin on my live site. [Note: I no longer recommend that plugin since it’s not maintained. Use Server IP & Memory Usage Display instead.]
I installed this plugin on my local development site as well and was totally blown away when I compared the memory usage on my local site vs. live site: the same site with the same plugins was using 34.13MB on my local server and 60.0MB on the live site. WOW!!!
How could this be? There must have been something in the server setup that caused the discrepancy. Googling some more, I read that PHP5.4 uses significantly less memory than PHP5.3, by a huge amount, on the order of cutting it in half. Sure enough, I checked and found out that my local server was running PHP5.4 while my live server was running PHP5.3. It seemed like a smoking gun, but I needed to do one more experiment to be sure.
I had another WordPress site running on Amazon EC2 with Amazon AMI, running PHP5.3. Memory usage for that was 47.8MB. After I upgraded that server to PHP5.4, it used 30.0MB. It was clear, switching from PHP5.3 to PHP5.4 saved almost 18MB of memory! That is huge!!!
My Number One Recommendation – Upgrade PHP
Based on these findings, my number one suggestion to reduce WordPress memory usage is to upgrade from PHP 5.x on your server to PHP 7.x, which is even faster and more memory-efficient. I’m now using PHP 7.3 on all of my personal and client sites.
If you are on shared hosting like GoDaddy, 1&1, Dreamhost, etc., there is often a button in the control panel to upgrade. Simple as that.
If you are running your own Linux server, it’s more complicated. Here is an article describing what I did to upgrade from PHP5.3 to PHP5.4 on Amazon AMI. (This procedure also works for upgrading to PHP5.5 and beyond). Note that there are other solutions to reducing PHP memory usage like Xcache. I’m considering that to be an advanced solution beyond the scope of this article. If you want to try that, go for it and let me know how it goes in the comments!
Be sure to re-test your site and plugins after you upgrade, as there might be some old code not compatible with PHP 7.x. I have not encountered problems with PHP 7.3 on my WordPress sites.
This Can Also Fix HTTP ERROR 500
On a different site, I was getting HTTP ERROR 500 when I tried to edit a page. Viewing pages on the front end was fine. I suspected some sort of memory issue. After trying a bunch of other stuff, I simply upgraded PHP from 7.0 to 7.1 and it solved the problem!
And Don’t Forget..
Of course, it also helps to delete unused plugins. If you want, you can install the Server IP & Memory Usage Display plugin and deactivate plugins on at a time to see their contribution to your memory usage. (Just remember to deactivate it when you’re done, because it contributes to memory usage as well). I can almost guarantee you that none will save you 18MB like switching to PHP5.5 or greater might do! But, eliminating plugins might be just enough to stop your memory errors from happening.
If you are not able to upgrade to PHP7+, and you can’t delete any more plugins, then you can always increase your allowable memory usage and/or get more memory for your server like the vast majority of articles recommend. The Server IP & Memory Usage Display plugin will tell you how much memory is allocated to WordPress (usually 40MB) and to PHP (usually 64MB or 128MB). You can Google around to see how to increase these limits.
But, I prefer to solve the root cause of the problem rather than treat the symptoms!
I hope my experience was helpful to you. Please comment on your findings and experiences about WordPress memory usage! – Brian
I am a freelance web developer and consultant based in Santa Monica, CA. I’ve been designing websites using WordPress and from scratch using HTML, CSS, PHP, and JavaScript since 2010. I create websites and web applications for businesses, nonprofits, and other organizations. I have a degree in Electrical Engineering (BSEE) from California Institute of Technology and a degree in Engineering Management (MSEM) from Stanford University.
Discover more from Web Developer Tips and Tricks
Subscribe to get the latest posts sent to your email.
Please Leave a Question or Comment
Thanks, it worked very well on my side.
Awesome, thanks for sharing! – Brian
A quick way to see if its one of many plug-ins using up either memory or IO is to temporarily rename the plugin folder on your worpress installation. This is particularly useful if you have many single sites running on a single hosting account, as you can rename the plugin folder to say xplugins and check the resource usage if nothing changes its not a plug in on that installation, if it fixes the issue you can remove the x and all plugins will be as they were before you renamed the folder, then you can do the same to each plugin (rename the subdirectory the actual plug in is in) until you resolve the issue. This can be time consuming but is a great option and works if you can’t access your wordpress site dashboard because of the IO or Memory usage – you only need access to your file manager in cpanel for example, which should aways be available whether or not your sites rescuers are maxed or not.
Agreed, great tips, thanks Natalie!
Best,
Brian
Hi…Brian…Awesome blog. But there are a lot of other steps I think you missed. 1. Increase the php memory limit in the php.ini file. 2. Using p2 or anything profile and checking plugins load time. 3 Using WP_Rocket or w3 total and enabling caching.
Hi Sumeet,
Yes, agreed, thanks for sharing!
Best,
Brian
thanks for your article
Hi Brian,
excellent man .. Thanks a toone .. from the bottom of my heart .. recently I changed my site protocol to https and started getting server 500 error .. most of the time the site was reaching full memory limit ! again googled and first page found your site … with fear I changed server settings to use php version 5.5 and wow .. it worked like a magic .. within fraction of seconds the memory usage came down !
Now I can see that my server allows to use PHP 5.6, 7 , 7.1 versions too. Question to you is shall I upgrade to any of these higher versions ? and if yes, what is downside of it. Will some of the plugins will not work ?
Rgds,
Mahesh
HI Mahesh,
I’m glad my article was helpful to you!
I need to update it though, haha. I have switched all of my personal sites and my clients’ sites to PHP 7.0 with no broken code (WordPress) so far!! PHP 7.0 is a significant speed and memory improvement over 5.6, so I’d recommend it!
Best,
Brian
Thank you!!!!! So much!!
Awesome Tweak, Thanks for the info. i have upgraded my wordpress sites’s php version to the latest (is..7.1) and got unimaginable results. Thanks man!
Hi Sandeep,
Great, thanks for sharing! Glad it worked.
Best,
Brian
Thanks for the info! As followed, But still my site use high resources. can you please help me deal with it. Please have a look at my site techflout.net
THANK YOU! I was shocked at how old the PHP version was on my own website. It’s been around for a while, but not long enough ago for the version it was running. Yikes!
Hi Valerie,
I’m glad my article was helpful to you! Thanks for sharing!
Best,
Brian
good Brian, also can recommend this to your viewers…
https://wordpress.org/plugins/server-status-littlebizzy/
for check the memory usage, yes PHP 7 is helping tremendous to our site
Hi All Super Technical Person,
Hope you are doing great.
I am getting CPU and RAM issue with WordPress running on RedHat machine.
Current system Configuration:
RAM: 8GB
OS : RedHat
PHP: 5.5
Mysql: RDS
Below are the error details:
Out Of Memory: kill Process 4890 (httpd) score 174 or sacrifice child
killed process 4890 (httpd) total-vm:3328056kB, anon-rss:1334320kB, file-rss:0kB, shmem-rss:68kB
oom_reaper: reaped process 4890 (httpd), now anon-rss:0kB, file-rss:0kB, shmem-rss:72kB
Please help me how to fix this error.
Also getting High CPU and Ram Utilization and web application goes down frequently
Hi mbmannubisht,
This is more a Linux issue than WordPress. You’ll have to find someone who is more of a Linux expert than I am!
Best,
Brian
WordPress in general compared to some other popular CMS just uses a really high amount of RAM. My WP sites consume between 150Mb to 400Mb of RAM each, but my PyroCMS or Expression Engine sites consume between 10Mb and 40Mb.
So I believe WP RAM usage needs addressing far more than anything else.
Hi Laurence,
The WP core doesn’t use nearly that much memory. You either have a memory-hogging theme or plugins, or some server setting issues. I would believe that WP uses more memory than other CMS’s though.
Best,
Brian
As i am using shared hosting, how can i see that what is consuming more memory? is there any way to look what plugin or file consuming more memory?
Hi,
I don’t know of a way to see plugin memory usage, other than deactivating them one at a time and measuring total memory usage. If you find an easier way, let me know.
Thanks,
Brian
Hi Brian,
Thanks for your kind reply. but where to go and see the memory usage stats? my hosting provider provided me only 32M memory limit. so i unable to use maximum plugins. if it hits the maximum limit. i am getting a fatal error exhaust memory limit.
Thanks in advance
You would have to install a plugin, like the one I mentioned in this article.
Also, make sure you are using the latest version of PHP, which would be in your hosting provider’s admin panel.
Brian
You can use this plugin to check memory consumption: https://wordpress.org/plugins/p3-profiler/
Dear Brian, we also release free Server Status plugin if interested:
https://wordpress.org/plugins/server-status-littlebizzy/
Memory usage, disk space, and more, in just a simple widget. Thanks for considering about our plugin and hope you are doing well… ciao ;)
Helo brian..
Few words from my experiences..
1. By choosing to switch to the newest php version is not the only solution but its true does the job.
2. To make low consume processing to ur server u need to think again what plugins do u use, some ppl will use extra security plugin for their wp cms web n this will make higher server processing so at least we need to use good cache processing system too we can use wp cache plugin or else we can install stand alone cache system in our server or else we can use cdn for better speed n low processing too.
3. We can use mariadb instead of mysql.
4. We can choose better dns provider. By using internal server dns system it actually make the server getting heavy to work.
5. No need to use external resources plugin such as social media sharing button esc.
Thx
Brian – this worked wonders. I was able to reduce the size of my site from 104MB to just under 40MB. Thank you SO MUCH for this guide!!!
Hi Jon,
Awesome! I’m glad it worked well for you!!
Brian