Question

In PHP, i am oftenly facing memory limit problem. Especially with highly resource integrated systems like Drupal, etc.

What i want to know here is:

  • Is it good to have very high php memory limit like 2GB?
  • Is there any major drawback?

Edited:

As a scenario, for example in Drupal, it NEEDS so much memory while we CLEAR the CACHE via Web Panel (Not by Drush or any script). So even for this case only, i am definitely needing high-limit around 512MB currently.

Was it helpful?

Solution

The golden rule: only give PHP what PHP needs to work, and nothing more.

This will prevent situations when your system needlessly clogs on the PHP process, and will also assist you in finding bugs (A script that takes 1GB of memory is unusual, and giving a 2GB of memory limit will hide that).


For exceptional cases, where you know a single script file is very memory heavy, you can use ini_set() to change the memory limit directive at run-time. See this and this.

OTHER TIPS

No, the more is not the better. It's the amount of memory a single script is allowed to consume. If your server contains, say, 4GB of memory, only two scripts simultaneously could eat all that memory.

In general, PHP scripts should use little memory. Normally all memory a script reserves is freed when it terminates, but you can clean up memory before that by explicitly unsetting variables that are no longer needed during execution. That way, you can make your script more memory-efficient.

If you have scripts that you know require large amounts of memory it's perfectly safe, and needed, to set the limit high. Problems can occur if the limit is set to high for the system in regards to available amount of memory and other running applications.

Also keep in mind that a faulty script that might end up in an infinite loop processing some data would often come to a halt quite fast if the memory limit is 64 MB or something like that. Having the memory limit at 4 GB in this scenario would leave you with a system running at full tilt in an infinite loop for way longer and might cause your entire system to become unresponsive.

But I have had large data crunching applications with memory limits of around 2-4 GB without any issues on systems with 4-8 GB of memory. Note that, as stated by other users, these applications was ONE instance. If you have applications that run several instances of your scripts (like most web-applications do) you will run into major problems by using a lot of memory in each instance. You will run out of memory and further clients will not be able to use your application until memory has been freed.

But don't just ramp up the memory limit to hide issues. Inspect why your application requires that much memory and see if you can rectify that first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top