Drush PHP Memory Limit

I was banging my head against the wall this morning because Drush kept telling me that I was exhausting PHP's memory at 128MB. I finally figured out the problem and thought I'd share for others searching the internet for a solution.

The first thing I did was check my php.ini file. Sometimes PHP on the command line will use a different php.ini file than the Apache version. In my case, the php.ini file is shared for both. To see the exact location of the php.ini file that Drush is using, use the following command.

$ php -i | grep php.ini
Configuration File (php.ini) Path => /etc
Loaded Configuration File => /private/etc/php.ini

This tells me that I need to edit the file located at /private/etc/php.ini to change the memory limit. I edited the file and changed the memory_limit value to 512M and then restarted Apache just to be safe. However, I kept getting the same error message indicating a memory limit of 128MB. To ensure that PHP on the command line was recognizing the new value, I ran the following from the command line.

$ php -i | grep memory_limit
memory_limit => 512M => 512M

This told me that PHP was in fact using the new memory limit.

Finally, I figured that Drush must somehow be overriding this value, so I began the search for Drush settings. In my case, I store my settings in my user directory, buy your case may be different. To find out for sure, run the following drush command.

$ drush status
 Drupal version                :  7.10                                    
 Site URI                      :  http://default                          
 Database driver               :  mysql                                   
 Database hostname             :  localhost                               
 Database username             :  root                                    
 Database name                 :  XXXXXXXX                               
 Database password             :  XXXXXX                                   
 Database                      :  Connected                               
 Drupal bootstrap              :  Successful                              
 Drupal user                   :  Anonymous                               
 Default theme                 :  xxxxx_omega                              
 Administration theme          :  seven                                   
 PHP configuration             :  /private/etc/php.ini                    
 Drush version                 :  4.6-dev                                 
 Drush configuration           :  /Users/nathan/.drush/drushrc.php 
 Drush alias files             :                                          
 Drupal root                   :  /Users/nathan/Sites/demo/apba    
 Site path                     :  sites/default                           
 File directory path           :  sites/all/files                         
 Private file directory path   :      

Take note of the "Drush configuration" value and that is where your configuration files are stored. In my case that is /Users/nathan/.drush/. When I looked inside that directory, I saw a file called drush.ini and immediately opened it. To my great relief, the first settings value was for the memory_limit and was set to... you guessed it... 128M.

After changing this value to 512M, all my memory problems went away.

As a side note, don't be too generous with setting your memory_limit for a production site. If you are having memory issues, it's best to investigate and look for potential memory leaks.