How can I increase memory_limit for batch processes only? 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.