WordPress’s Memory Usage, should it be one of your concerns?
As a WordPress end-users, you might never care about the term “Memory Usage” until you encounter a fatal error that prevents your site from working:
Fatal error: Allowed memory size of … bytes exhausted (tried to allocate … bytes) in somefile.php: …
Don’t panic just yet, you’re not alone: http://www.google.com.vn/search?q=wordpress+allocated, other WordPress users are having the exact same problem.
The term Memory Usage (in WordPress’s case) is used to refer to how much memory your plugins, themes, and WordPress core files consume when they get parsed and then processed server-side.
WordPress’s core files (3.0’s Twenty Ten theme installed) consumes about 13MB of memory (on some hosts with added modules or features, this number can be even higher. For example, it is 16 instead of 13 on one of my websites). Of course, you would have no problem installing WordPress on any host, because 13MB is easily satisfied (most hosts offers a 64MB or 32MB limit by default).
Exceeding the limit
The problem is, you are not happy with the default WordPress installation with limited functionality, you need some plugins and, obviously, a theme.
A web developer told me that he couldn’t live without at least 30 plugins, so let’s take a rough assumption of an average number of installed plugins of 20 (plus 1 theme, a special kind of plugin).
A plugin can be very small (for example, Hello Dolly – a dummy plugin created by Matt Mullenweg – only adds 6KB to memory usage), or can be very large (for example, GD Star Rating 1.9.x – a popular rating plugin – adds more than 4MB to memory usage).
Although the calculation should base on how a plugin or theme is loaded but let’s again take a rough assumption of 1MB memory usage used per plugin, we shall have the fully-loaded WordPress installation that consumes more than 33MB.
If your host is configured to use the 32MB memory limit, you will surely be greeted with the fatal error message we are discussing about.
When you got stuck with such error, you will certainly pay Mr. Google a visit and you will find some straightforward solutions regarding changing such limit. If you come to this page thanks to Mr. Google, I will gladly quote those solutions so you don’t have to go find elsewhere.
So, you can try:
memory_limit = 64Mto your server’s php.ini if you have access to it
ini_set('memory_limit', '64M');to your wp-config.php
php_value memory_limit 32Mto your .htaccess file
(Note that, however, if you’re on a shared hosting system, most of the time you won’t be able to change the limit using the above methods. You will have to ask your hosting provider to change it for you.)
General speaking, the above solutions work, but they are not recommended, why? Because the more you raise this limit, the sooner you fall into a common pitfall: too much work for the server. A higher memory limit is a poor substitute for good coding and can be very problematic, especially for high traffic websites.
In case you guys don’t know, each time a visitor clicks on something on your website, he or she is telling your server to work hard and output some kinds of result. A server is nothing mysterious, it is a computer, with hardware, software, RAM, etc. and they consume power, a lot of it.
One of the things you probably notice when registering for a new host is the CPU usage limit (consumed by PHP scripts parsing and possibly other processes). Such limit is set differently for each hosting company, for example, HostGator allows a 25% CPU Usage limit – http://www.hostgator.com/tos/tos.php. More visits, more clicks, and more script parsing will demand more CPU usage. Your website might be inaccessible, or worse, got disabled by the hosting company for “consuming more system resources than allowed”.
Now that I am writing this not to scare anyone to death, it is a common mistake in many websites nowadays, too much contents requiring too much work from the server. Some websites use about 61MB memory and make about 126 queries to the database. 61MB is way too high for every page load, don’t you think? I myself never use more than 30MB memory and 40 queries for a single page (except for some specific situations or specific pages, and FYI, BetterWP.net only uses an average of 16MB memory and 25 queries per page), 126 is a figure I simply can not imagine.
There are some obvious solutions that you can apply without waiting for plugin developers to do their job:
- Use fewer plugins and well-coded themes. Don’t get me wrong, this is the best solution and require professional work. Some useful tips and articles about avoiding using plugins, if you are interested: topic/plugin-alternatives/
- Use cache plugins. All the high traffic websites use them. I will recommend WP Super Cache or Hyper Cache if you ask me.
There’s a somewhat hackish solution that I called Selective Plugin Loading. I have written a follow-up post regarding such solution, and you can read it here: Selective Plugin Loading.
The ideal scenario is, of course, the plugin authors change the way they load their plugins, by loading only when needed or, even better, allowing users to choose when to load. This is such a big favour to ask from all plugin developers and I do believe it would need some kinds of APIs or at least an official guide and announcement from the WordPress team, otherwise plugin developers will unlikely take any action.
Anyone who has opinions about this problem please comment below. If you guys would like to ask about the selective plugin loading solution, comment on that article would be ideal. That’s all I have to say, thank you for your time, be safe and stay away from memory-eating plugins and themes 😉