Making your Drupal modules use less memory

Drupal's design (as of Drupal 5.5) is that every enabled module is loaded into memory on every page view. This is a convenient and fundamental feature of Drupal's design philosophy­­­—it allows modules to merge their functions into a rich and useful API for the site. However, it also can make a Drupal site use a lot of memory, and the time it takes to load in all of the modules on every page view can come to dominate the amount of time it takes generating each page, causing Drupal to use up a lot of CPU on the web server.

Drupal 5 has an advantage over Drupal 4.7 in that installation related code is now in a separate .install file which doesn't normally get loaded into memory. However, it can still be advantageous to write your modules in several separate files, with the .module file acting as a function call router of sorts -- loading in the necessary sub-files only when necessary. A function which previously looked like this:

function do_something($arg)
// Lots and lots of code here
can be replaced with one that does this instead:
function do_something($arg)
include_once dirname(__FILE__).'/';
return _real_do_something($arg);

Unfortunately, this kind of pattern is at the moment used only infrequently, so on a Drupal site which has many modules enabled, memory and page generation speed can be dominated by loading in a lot of code which will never get used.

Some of the modules that I've created are simply applications of this low-memory pattern to existing drupal modules. If you have a Drupal site which suffers from high memory or CPU usage problems, you may be interested in perusing through them.



My site with high trafic has memory problems even on a VPS set to 4000MB so I will be happy to try your modules.

Do you know how I could find which one of the installed modules on my site is causing problems ?


If I understand well the concept, reducing the memory usage by reducing the size of the files loaded automatically but increasing the disk reading usage as for each of your functions you need to include a new file...


Am I correct?

Subscribe to All Posts - Wesley Tanaka