Why does Drupal have such a large memory footprint (and how can I reduce it)?
Hi-
I've installed Drupal and am about midway through adding modules and it seems like Drupal is using an awful lot of memory.
Right now, there is virtually no data in my install, and I am the only one hitting the system, but I see that there are 4 apache processes each using 30MB of memory. This seems to be a lot heavier than other apps I have used, especially since the system is not really doing any work aside from me going in and changing settings and creating sample content items and users and roles to see how the system responds. Performance is reasonably fast, but it should be fast with only one user.
I've seen posts where people have indicated generally that "the more modules you have, the more memory Drupal needs" but I don't see why a fresh install with virtually no data would need to allocate 120MB of memory to itself just for one user, regardless of how many modules are installed.
I really like Drupal so far compared to other systems I've used, but I'm mystified by the resource utilization. Can someone give a little more detail as to why Drupal needs so much memory and maybe how I can trim the requirements?
Thanks.

30MB not 120MB
Just a note: you can't really say that Drupal is using 120MB - it is more accurate to say it is using 30MB. Your Apache is tuned to run 4 entire copies at once - that is purely an Apache configuration decision. 4 processes probably isn't a bad number though - it all depends on how many processes you want available at any one time.
Pretty much the number and size of modules (eg the amount of code running) determines the amount of memory - the amount of data you have or the amount of work your site is doing won't have much effect until it gets much larger.
Trimming memory usage is really just a case of disabling Drupal modules (or choosing slimmer alternative modules), disabling unnecessary PHP extensions, and disabling unnecessary Apache modules.
Generally large modules with complex configurable UI will use more memory. If you are loading a large configurable module to make a few small changes to your site, it might be better to code up a small custom module that just makes the specific changes you want instead.
--
Anton
I hear what you are saying.
I hear what you are saying. On this box Apache has about 12 MinSpareServers at any point in time and so the four 30MB processes are the ones my requests are hitting so you could think of them as approximate "copies" of each other. But from an absolute resource standpoint, the server is allocating 120MB of memory for me as just one user.
But let's go with 30MB - what is Drupal doing that it needs 30MB of memory for just one user in a system with no content?
I have about 20 modules enabled - so that averages more than a meg per module - where is all this memory going? 1MB is a lot of memory for a PHP script to reserve. Where is it going? What are the modules storing in memory when there is no data in the system? What files are being read into memory? What data is being read into an array? Are certain modules reserving chunks of memory for future use?
Where is the 30MB going? Is there a way for me to check and see the memory usage of each module?
No one can answer this?
No one can answer this?
_
You might have better luck getting the eyes that can answer the question in the drupal issue queue or irc-- but i recommend you don't yell at them when asking.
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
Sorry, the bold text was just
Sorry, the bold text was just for emphasis, I didn't mean for it to be read as shouting. (I thought all caps was shouting)
I am not trying to be accusatory, I just want to understand some of what is happening behind the scenes so that I can be an effective sysadmin for a box running Drupal. I feel it is a legitimate question to ask - the answer would probably be useful to many others as well.
I will also log an issue for this - thanks for the suggestion.
_
no worries-- online communication is still an imprecise method. I just wanted to give you a friendly warning cuz I know bold, all caps, or excessive punctuation is not generally received very well on d.o. ;-)
_
Don't be a Help Vampire - read and abide the forum guidelines.
If you find my assistance useful, please pay it forward to your fellow drupalers.
What modules are yon using?
What modules are yon using?
http://drupal.org/node/493166
Enabled modules: Content
Enabled modules:
Content Access
Content-CCK
Content Copy-CCK
Fieldgroup-CCK
Node Reference-CCK
Number-CCK
Option Widgets-CCK
Text-CCK
User Reference-CCK
Book
Color
Comment
Help
Menu
Path
Profile
Search
Statistics
Taxonomy
Upload
Block-core required
Filter-core required
Node-core required
System-core required
User-core required
Watchdog-core required
Tables API
Token
uBrowser
Cart-Ubercart
Order-Ubercart
Product-Ubercart
Store-Ubercart
Attribute-Ubercart
Notify-Ubercart
Payment-Ubercart
Reports-Ubercart
Roles-Ubercart
Cart Links-Ubercart
Product kit-Ubercart
2Checkout-Ubercart
Authorize.net-Ubercart
Credit Card-Ubercart
Configurable Content Links-WorkflowNG
Per-Entity Logs-WorkflowNG
State Machines-WorkflowNG
Workflow-ng-WorkflowNG
Workflow-ng UI-WorkflowNG
Recurring Payments-Ubercart
Test Gateway-Ubercart
Hmmm... when you add up all the submodules for CCK, Ubercart, and WorkflowNG it totals out at 48 modules. That works out to an average 600K per module.
Data in this system:
*3 user accounts (including default User1 superuser, but I am the only person logging in)
*5 roles (including default Admin, authenticated and anonymous roles)
*8 content items
*default UI theme
*1 test comment
*2 Ubercart products
*1 Ubercart product kit
*no custom CCK fields
*no custom WorkflowNG rules
And I haven't even enabled all the modules I want yet. I am still curious about how Drupal modules allocate memory, and would appreciate any details you can provide on what is happening behind the scenes.
Thanks!
Drupal doesn't allocate memory
PHP is responsible for allocating memory.
All Drupal does is load a whole bunch of PHP files (from core, modules and the themes involved) into memory. What actually gets done beyond that depends on what those files do. There are data structures loaded for the menu hierarchy, config settings, caches, nodes, taxonomy etc etc. There aren't many people that could go into exhaustive detail about this, and they probably won't be reading this post anyway.
Some pages require a lot more memory than others - usually the big ones are certain pages in the admin section (eg module listings etc). You can use the devel module to see how much memory any particular page used.
As far as I'm aware (you'd need to check this yourself as I don't know for sure), mod_php doesn't release the memory used by Apache processes. So if a certain child process is used to load an expensive page and ends up allocating itself 30MB, that process will still be allocated 30MB even if all its subsequent requests require half that. To mitigate that, you'd need to lower the lifetimes of the Apache processes so that on average less of them eventually hit the expensive pages.
At this stage in your sites development where most requests are from you and are in the admin section, each process will be showing a lot of memory. When much more of your traffic is outside traffic (especially with cached anonymous traffic), there will be smaller proportion hitting the expensive admin pages.
If you really really want to minimise RAM usage and have exhausted all Apache tuning methods you'll need to investigate alternative web server configurations. eg the Apache prefork model and mod_php are renowned memory hogs. I don't know if PHP is thread safe yet though, but I imagine the threaded models would use far less duplicated memory. And you might even want to look at FastCGI and other web servers like lighty or nginx etc
But a very flexible extensible system like Drupal will always use a lot more memory than one that is coded to do very specific things. A lot of Drupal modules are intended for non coders (or coders that want to save time) to be able to do complex tasks just through configuration UIs - eg CCK, Views, WorkflowNG etc etc. The very flexible configuration of these modules uses much more code (and memory)
Sometimes you may be loading a number of large modules to do a couple of small things that could be replaced by one simple 'hardcoded' custom module that uses next to no memory.
It's a tradeoff between the cost of development resources and the cost of deployment resources, and usually the deployment resources are cheaper. You should only spend development resources on optimizing stuff you have identified as a real world bottleneck that is more expensive or impossible to fix with deployment resources. But in terms of deployment costs, RAM generally isn't an expensive bottleneck to fix compared to other bottlenecks (eg database). I suspect you are worrying about this prematurely.
Not many people really worry too much about memory usage with Drupal - most concerns are with the levels of DB activity. Once you install the devel module - you'll probably forget about memory and have a whole new thing to focus on :)
--
Anton
Thanks for this info.
Thanks for this info.
Measuring memory modules consumption
@SerenityNow: I don't know about Devel, but you may be interested in these:
http://2bits.com/articles/measuring-memory-consumption-by-drupal-bootstr...
http://2bits.com/articles/server-indigestion-the-drupal-contributed-modu...
Thanks. Very informative
Thanks. Very informative articles and website.
Same here. I've been having
Same here.
I've been having similar problems and worries.
I'm building a site for non-profit organization whose budget is very limited, and I'm doing it all for free. I was amazed at the power of Drupal and the way I could tailor it to our needs. However, I am currently lost as to how I can accomplish what I need to do, based on our shared hosting which is limited to 32mb PHP memory. We can't afford a virtual host.
I'm not using a lot of modules. Most of what I have installed is basic stuff:
Can't do without
CCK (with Date, Email, Link & Filefield)
Views
WYSIWYG & TinyMCE
Poormanscron
PathAuto
Auto Nodetitle
Essential for a community-driven site with mostly private content
Content Access
Taxonomy Access Control
Upload Path
Token
Content Profile
Better Formats
Webform
Useful
Admin Menu
Advanced Help
Calendar
Taxononomy Super Select
This is all I have installed, and it would be very difficult for me to deactivate any of these modules. My modules page reports almost 30mb of used memory (via print memory_get_usage), and that's when I don't get a WSOD. I haven't even added an image gallery functionality, ImageAPI, ImageCache or anything like that.
I've done some tests and the modules which take the most memory, by a long margin, are Views (8mb) and CCK (4.5mb). Next in the list, just above one mb, are Admin Menu, Webform and Calendar. All others take up less than 1mb.
Right now my only viable solution is to freeze my Views and figure out a way to hard-code the generated queries into my template file or something. But I don't even know how to do that. Views is an amazing piece of work, but I don't know how I can keep running a module which takes up a quarter of my alloted server RAM.
Is anyone using Drupal on a 32mb shared host without limiting him-or-herself to Core?
Any ideas would be most welcome.
You may be able to use
You may be able to use ini_set in the settings.php to increase the memory limit. Some shared hosts allow this, some don't. Prob your best option is to find good value hosting rather than try to manage the amount of memory drupal is using.
All shared-hosting options I
All shared-hosting options I found in France were limited to 32mb, non-neogtiable. It seems we'll have to go the virtual server route in order to have the functionality we need. But this is an extremely costly option for us. I've spent the last two weeks speed-learning Drupal 24/7 and last night I've already started looking unhappily sideways at other options. I'm down, as the poet said.
look for a drupal specialist hosting provider!
I posted about this in the forums when we had major problems with 'so-called' VPS solutions.
Since we moved to Hotdrupal our site we've not looked back. We have an increasing set of modules and a huge memory footprint (we often exceed 128MB), but the hosting plans have been very flexible. For what you are proposing one of their cheap hosting solutions would probably be more than enough. Plus you get a real support person to deal with queries (quite often Steve, the founder and excellent sysadmin). Total downtime due to hosting in the past year = 3 mins (and only because they told us there might be a reboot, I wasn't up to check). http://www.hotdrupal.com/plans.html
Mention that Stephen from the UK charity Go Help recommended you!
=-=
poormans crons is overhead you don't need. why not set up a cron job on the server rather than subject yourself to a module that isn't required?
32M isn't alot of memory, core will likely run on 32M alone.
imageapi module for example reccomend 96M of memory as image manipulation and such can be intensive.
many shared hosts can be restricting because of their RUP. The more powerful the script your run, the more you will need a VPS or DED.
you're views can be exported and turned into custom.modules though this likely won't help with memory.
Thanks for the comments and
Thanks for the comments and suggestions. I'll check Hot Drupal hosting, however I think I found a host in France which allows a 96mb memory limlit on their shared hosting plans, www.haisoft.fr. They are also specialized in Drupal, and they will configure cron jobs for you (I was perfectly aware that getting rid of the poormanscron overhead was the way to go). Many hosters in France do not allow cron jobs.
It seems that Drupal is a very different beast from all other CMSs. I got practically trashed by another hoster with whom I've corresponded who said that 32mb is already a huge amount of memory for a CMS and that I should look into private hosting instead. Only people at Haisoft were not surprised, since they use Drupal a lot.