Fixing “Memory Exhausted” Errors In WP-DBManager

WP-DBManager is a handy plugin that can, among other things, make periodic database backups and send them to a specified email address. I installed it on this blog months ago and up until a week ago everything was working perfectly. Then one day the backup emails simply stopped coming.

What Went Wrong?

A quick check of the server’s error_log revealed the problem: the plugin was running out of memory.
[19-Aug-2010 15:47:44] PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 11145167 bytes) in /home/foo/public_html/wp-content/plugins/wp-dbmanager/wp-dbmanager.php on line 101
[20-Aug-2010 15:47:49] PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 10929395 bytes) in /home/foo/public_html/wp-content/plugins/wp-dbmanager/wp-dbmanager.php on line 101
[21-Aug-2010 15:47:38] PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 10693507 bytes) in /home/foo/public_html/wp-content/plugins/wp-dbmanager/wp-dbmanager.php on line 101
Alas, this was not all that surprising. Due to the way the mail() function works in PHP, WP-DBManager must read the entire backup file into memory before emailing it. Since the database gets bigger and bigger with each new post and comment, it will one day inevitably exceed the maximum amount of memory that PHP scripts are allowed to use and thus crash the plugin.

There are many ways to fix this problem, but most of them will only buy you some time. Increasing PHP memory limit will work in the short-term, but eventually the WP database will grow large enough to exceed the new limit. Similarly, cleaning up the DB – e.g. by removing old post revisions – can make it small enough that the backup file fits in the available memory. But that, too, is only a temporary solution.

The Fix

Ideally, the plugin shouldn’t even need to read the entire file into memory. It’s just the “convenient” design of the built-in mail() function that demands this. To solve the “allowed memory size of XXX bytes exhausted” problem once and for all, we need to replace mail() with something more flexible.

So, after an hour or two of hacking, I managed to rig WP-DBManager to use the free Swift Mailer library instead. Among the library’s many features is the ability to send file attachments of virtually any size without worrying about memory limits. Thus enhanced, the plugin should be able to handle large backups without a hitch.

You can download the fixed version below. I’ve successfully installed it on my site and the periodic backup emails are rolling in again (yay!). Note, however, that due to the inclusion of the Swift Mailer library the plugin now requires PHP 5.2 or later. Make sure your server has that before trying to install the modified version.

wp-dbmanager-modified.zip (190 KB)

Related posts :

4 Responses to “Fixing “Memory Exhausted” Errors In WP-DBManager”

  1. JB says:

    What about the problem now with

    Checking PHP Functions (passthru(), system() and exec()) …
    passthru() disabled.
    system() disabled.
    exec() enabled.

    The plugin no longer works, any fix for that?

  2. White Shadow says:

    I think that’s something you should ask the plugin’s author, not me. I only wrote the memory-related patch.

  3. JP says:

    This plugin has effectively been abandoned — the author posted that he’s out of school and working full time. Very little time to work for free. Anyway, I started using BackWPup, it can send backups to my Amazon S3 account. Better to backup offsite anyway. Just curious though, what was the size of your backup file when it started hitting this limit?

  4. Jānis Elsts says:

    Around 15 MB, I think. I’ve also switched to a different plugin now – BackupBuddy.

Leave a Reply