Support » Fixing WordPress » Running out of memory

  • I’ve got a test WordPress install on a VPS for a new site I’m developing. The site isn’t getting hardly any traffic yet other than myself putting in some content (through WordPress) and Google crawling the site. Yet, at least once a day I fully run out of memory on a 2 GB VPS.

    Apache has several processes running, each eating up about 500MB of memory, consuming all physical and virtual memory to the point that I can’t even kill the process and reboot. I’m not seeing excess traffic.

    I’ve seen at least two other people mention having similar memory consumption issues since upgrading to WordPress 3.4. Is there a good way to determine from Apache or PHP logs perhaps what is eating up my memory?

Viewing 7 replies - 1 through 7 (of 7 total)
  • I’m terrified to launch this site publicly because this is happening with almost no traffic hitting the site right now.

    I’m running apache2-mpm-prefork with stock settings from Debian. My site config has a lot of rewrite rules in it for WordPress, caching and security. I’ve moved those to 000-default.conf and out of .htaccess to help with performance. My 000-default.conf is here:

    And my enabled Apache modules are:

    alias auth_basic authz_host authz_user cache cgi deflate dir env expires filter headers include info mime negotiation php5 reqtimeout rewrite setenvif speling ssl status

    I suspect it might be a bad WordPress plugin and PHP causing all the problems. I’m tempted to crank down my PHP settings just in case.

    The worst case is what is happening where Apache eats so much memory the entire system is unresponsive and does nothing. Can I write a basic script that checks memory consumption and restarts the apache2 service if need be?

    Or can someone help me and point me to how I would identify what is causing the memory to spike every once in a while? Would there be something in the Apache logs at some point?

    Did moving your .htaccess rules to the config show any improvement? Reason I ask, is those rules (from viewing it on pastebin) could definitely use some refining. Lots of wasted processing cycles. Plus using RedirectMatch along with RewriteRule has known to cause issues. Especially when it comes to placement. They can affect each other or cause either not to work at all.

    I would try to stick to just using mod_rewrite. A lot of those RedirectMatch’s could be easily converted to RewriteRules.

    I would redirect to a site that has more experience with rules like that but they probably wouldn’t help you much, because you have so much going on there and I doubt anyone is going to have the time to redo them for you. 🙁

    For example:

    RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
    RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]

    Those rules are the same thing. The second line was developed by g1smd as the first one is very inefficient with the usage of the “everything atom” or “.*” (actually lines 75 through 82 can be optimized to just one line). I can find hundreds of other examples where those rules could be optimized. But I’m not sure if that’s really causing your problems, but optimizing them would make a big impact in the long run.

    And the usage of this [F,L] only needs to be [F]

    The [L] flag is implied with the [F] flag. So there is a wasted cycle everytime it’s used.

    Have you tried switching to the default TwentyTen or TwentyEleven theme? What about deactivating all plugins?

    You could try this with all of you plugins activated to see which one is consuming the most resources:

    Sorry, that’s all I can think of right at the moment.

    I assume a lot of the rewrite rules are somewhat redundant. The second I fired up the VPS, I was getting hit by would-be hackers in Romania, Thailand, Taiwan and China. I’ve installed a few WordPress security plugins who added such rules, as well as followed this guide:

    I think moving the rules to my config and out of .htaccess did have a noticeable impact, but I also assume I could benefit from consolidating those rules.

    If you’ve any more advice (such as how to change the RedirectMatch rules) that would be great.

    My rules are duplicated under <VirtualHost *:80> and <VirtualHost *:443> so I really only have half as many as it looks like.

    Also, which version of Apache are you on? I noticed some rules that will only work on Apache 2.x.

    I’m on 2.2.22.

    You mentioned coming 75-82 and how 75 and 76 were redundant. I’m looking at a regex basics guide, and I’m assuming I’d combine them like this:

    RewriteCond %{QUERY_STRING} ((<|%3C)([^s]*s)+cript.*(>|%3E)|(<|%3C)([^e]*e)+mbed.*(>|%3E)|(<|%3C)([^o]*o)+bject.*(>|%3E)|(<|%3C)([^i]*i)+frame.*(>|%3E)) [NC,OR]

    For those lines that I mentioned above:

    Also wherever there is [F,L] can be just [F] because [L] is implied with [F]

    You could also remove the ifModule(s). They’re a wasted resource. All they do is tell Apache that if the rules don’t work, then fail silently. Which is good when you’re distributing the code as the author will have less to worry about with it breaking sites, but in all practically they are just a waste on a live site.

    Converting the RedirectMatch(s) to RewriteRule(s) would take some time. I’m pretty sure Jeff at Perishable Press has spent some time on those rules. (I would convert them for you but that would take more time than I have right now. You may try moving them above all your RewriteRules. I believe the order of things are:

    Access controls first.
    Redirects next. Within the list of redirects, most specific first, most general last.
    Rewrites last. Within the list of rewrites, most specific first, most general last.

    That’s what I was taught anyways. I know that from experience it sometimes doesn’t always work out best like that. You really just have to work with it to find out what works best for your setup.

    Thanks! I did remove the ifmodules earlier realizing they are pointless. I know I have the modules loaded. I also removed the IP-based denies and moved them to my firewall.

    I’m also going to experiment for a few days with Apache worker as opposed to prefork (which has also necessitated moving to php5-cgi). Everyone says it uses less memory and is faster, but right now it appears to be using more memory.

    I’ll move the redirects up above rewrites.

    I’ll also check out the p3-profiler plugin tonight.

    Here are my new rules, a little shorter:

    I’ve also thought of trying nginx, but that means converting all those rules and honestly I’d be lost.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Running out of memory’ is closed to new replies.