Memory

My #1 Trick to Fix WordPress Out of Memory Error or HTTP 500 ERROR

I was working on a client’s new WordPress site when I got the dreaded Linux “out of memory” error in my browser unexpectedly:

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

I had never gotten this before, so I Googled around. Almost everywhere I looked, the advice was 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 my allowable memory though. 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

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!!!

TPC Memory Report
This report shows my memory usage before and after upgrading to PHP5.4. The difference 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 sites and PHP 7.2 on my current clients’ 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 PHP5.5+, 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



58 thoughts on “My #1 Trick to Fix WordPress Out of Memory Error or HTTP 500 ERROR

  1. 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

  2. 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!!!

  3. Thank you so much for posting this – WP can be such a minefield sometimes! I’ve had the same problem after uploading another plugin and since I couldn’t really part with any other, I took the plunge and updated the PHP from 5.3 to 5.5. So far so good and the memory has gone from 97% to 57% instantly.

  4. Wow! After looking for hours through forums, Stackoverflow and the like I was fed up since none of the solutions there worked. And this “hack”, simple as that, magically solved the memory issue, thank you a lot!

  5. Had a small query.
    My website is currently utilising around 120MBs of RAM after the update of PHP to 5.5.
    Is that normal?
    When I wrote my last comment here, I just had updated to 5.5, it was around 60 MB.
    What are your thoughts?

    1. HI Dev,

      Hmmm, that seems strange. Try deactivating your plugins and reactivating one by one to isolate the offender.

      Best,
      Brian

  6. Hello Brian,
    It worked perfectly!
    It was really simple.
    However I will monitor my site for errors and will report here if any pops up.
    I have bookmarked your blog, must check it out from time to time to know some cool tricks like these.
    Keep up the good work.

  7. I can’t believe this. I updated PHP in cPanel, and it works! Why you hear no one about the major change in this 5.4 .. !?

    @Rod: in my case it is gonna save lifes.. We where peaking with a WordPress website (ughhh) … and this caused funny errors…

    1. Hi Matthijs,

      Yes, isn’t it amazing? I don’t know why it’s not more widely publicized, but that’s why I wrote this article.

      Best,
      Brian

  8. All,

    Be aware that various plugins used to provide you with information on your PHP environment will and do provide false readings. This means that if you choose to place a PHP directive to increase memory limit for example and your shared hosting does not permit this with your package and most don’t the plugins will happily tell you that you have say 256m and 64m upload limit when NOPE you don’t! Even a PHP Info statement will return settings that reflect your PHP directive and NOPE you really do not have that allocation. You would need to check by using the tools provide in your hosting control panel to really know what the PHP environment is truly set to.

    Bottom Line is: DO NOT RELY ON PHP PLUGINS OR PHP INFO TO PROVIDE THE REAL SETTINGS!

  9. Hi Darpan,

    Believe what you wish. That is a free plugin which I did not write. I gain nothing by promoting it.

    I promote plugins in a lot of my articles because I find them useful and want to promote open source development.

    Brian

  10. Seriously? I had no idea that upgrading PHP (5.3.3 -> 5.6.15) had such an impact on memory usage. My wordpress site was using about 80 – 90Mb of RAM per page load and now it is down to ~25Mb. My site is on VPS which has 1Gb of memory.

    I initially though that the lack of performance was caused by the site itself. I also run a similar site on shared hosting which didn’t have this kind of performance issue and it turns out it had later PHP version installed.

  11. My blog went from 96mb memory footprint to 60mb by just switching from PHP 5.3 to PHP 5.5, as easy as that!

    Now to find what is causing the still high 60mb… It is not the plugins, it seems, because even disabling the heaviest plugin (according to P3 plugin checker) it’s still 60mb.

  12. Thanks for this article. I upgraded to PHP5.5 from PHP 5.4 but My site is not showing any improvement. I am using Query Monitor plugin and it is showing that my site is using peak usage 256mb on RAM which is insanely high.
    Can you tell me how I can reduce RAM usage? I have pingbacks, trackbacks, wp-cron disabled. I am not using any plugin that is known for memory leak.

    1. Hi,

      Wow, that is an insanely high RAM usage. The theme might be causing the problem. Try changing to TwentyFifteen or other standard theme.

      Plugins are the other possible culprit. Even though none has a reported memory problem you never know. Try disabling all and see what happens.

      Brian

      1. You won’t believe it, but my host was arguing with me that the high memory usage on my WordPress websites were a “development” issue *cough cough*. I sent them the link to this article, and the one server admin guy (always been uber-helpful) said he’s going to upgrade my reseller hosting server to PHP 5.5.

        He did – results: 50% reduction in memory usage across all my sites! :)

        Thank you so much for this!

      1. I think you should be careful with sweeping statements like “cutting it in half” Brian.

        I have a WordPress site on a dedicated server with 32GB RAM and it’s generally using anywhere between 15 and 25GB of RAM depending on server load.

        Looking at our logs (we switched to PHP 5.5 a couple of months ago) the memory footprint hasn’t really changed at all.

        A change in PHP version can introduce many many problems in itself so be very very careful when doing it, the figures you quote as “huge’ above are only huge in proportion to the fact that you are on a tiny server.

        Here in 2015 it’s hard to find a server with that small a spec nowadays, the average VPS for a handful of small change a month usually comes with 512MB and the vast majority of our clients websites are using servers with many GB of RAM.

        I would suggest that for a large majority of people saving 18MB of RAM would make no difference at all to their system as it is a TINY amount of RAM overall.

        1. Hi Rod,

          Great points, thanks! A lot of people are on shared hosting, where you generally get a lot less memory, so that’s where this can help.

          Thanks,
          Brian

Leave a Reply to Brian Shim Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.