I have a batch process that occasionally exceeds the memory_limit, which is currently 96M. Of course I know the ways to increase the memory limit for the whole site, but I prefer to increase the memory limit for batch pages only.

One way I could think of, is to implement hook_init in a custom module and do ini_set('memory_limit', '200M'); whenever arg(0) == 'batch', but I'm not sure if that is the best solution.

What do you recommend? I guess I could do it in .htaccess, but regex isn't exactly my forte...

[update] Meanwhile, more searching tells me (Stack Overflow, Webmasterworld) that it is not possible to add a conditional php_value directive to .htaccess.


I think that @kantu's answer is the proper thing to do, but I am going to offer up an alternate solution that is a little hackish.

If you don't want to do a module, then you could add the setting to settings.php wrapped in a conditional. Just note that this is included really early in the bootstrap process, so you don't have access to the vast majority of the API.

You could try adding

if (strpos($_SERVER["REQUEST_URI"], "/batch") === 0) {
  ini_set('memory_limit', '200M');

to the file.

I would also double check the PHP doc about memory_limit (or post a question on one of the sister sites). I believe that PHP doesn't allocate this during startup; it is just a safety limit to prevent imploding your server. I am pretty sure you can just bump up this limit globally.

If you are worried about Apache hanging on to this memory, then you can play with the MaxMemFree setting for Apache. A while ago, I did a site that would need huge amounts of memory very infrequently. Tweaking this parameter, along with a MaxRequestsPerChild, kept my server running happily.

  • Thanks for the tip, I didn't know about MaxMemFree yet. AFAIK, you're right about memory_limit: PHP will only use the memory it needs. Mar 6 '12 at 15:44
  • I realize this is an old question / answer... but I was searching again for the proper way to do this. Acquia recommends this method of using the settings.php file. support.acquia.com/hc/en-us/articles/…
    – Craig
    Oct 8 '18 at 19:32

I had similar requirement , but for admin/build/modules pages as it consumed more memory

function memory_setup_init() {
 //Check for pages goes here
  if (...) {
    ini_set('memory_limit', $memory_limit .'M');

My modules pages required 150MB sometimes as it was heavily loaded. I suggest this approach as you can set up a form and you will have ability to turn it on/off or increase on the fly.Hope this gives you some idea to continue.

  • Yes, that is exactly the solution I referred to in my question. I'm sure it would work, but my gut feeling tells me that a custom module (even this small) is overkill for modifying a single php setting. Mar 6 '12 at 12:18
  • For my case it was required to be a separate module as I had one separate Form (system_settings_form) where I could group some pages and set memory for it, else you can make use of any existing modules.
    – GoodSp33d
    Mar 6 '12 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.