Is the more the better for PHP memory_limit? [closed]
Asked Answered
L

3

28

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.

Ljubljana answered 12/10, 2012 at 11:50 Comment(5)
In a word, NO. Your memory limit should be set as low as possible within the bounds of what you need. The memory limit is applied to each individual concurrently executing script, not globally. Setting it low will help ensure that if you make some mistake that causes e.g. an infinite loop, the script will be killed early rather than spiralling out of control and grinding the entire server to a halt.Unpaid
How much memory will you need serving 100 concurrent requests? 200GB?Arnoldarnoldo
@Arnoldarnoldo The memory limit does not define how much memory is actually used. It's just the limit a single process is allowed to use.Curhan
EDITED MY QUESTION PLEASE. So oh god!!! now my system is slowing down because of that limit??? (only for Clearing the Cache Process apparently)Bursar
@4lvin memory_limit is PHP_INI_ALL changeable, so if you have a specific script that needs 512MB then call ini_set('memory_limit', '512M'); at the top of the script.Unpaid
M
43

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.

Mccallister answered 12/10, 2012 at 11:53 Comment(2)
EDITED MY QUESTION PLEASE. So oh god!!! now my system is slowing down because of that limit??? (only for Clearing the Cache Process apparently) @_@Bursar
Thank you. You solved even TWO problems ;)Bursar
S
5

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.

Shannanshannen answered 12/10, 2012 at 11:55 Comment(7)
unset() is usually not requiered as long as you'll not use global variables all the times.Curhan
@Curhan Even that shouldn't make a difference should it? global is still only global within the confines of the script execution, they don't persist between requests. Note also that unset() does not guarantee the memory will be freed immediately, you'd have to call gc_collect_cycles() as wellUnpaid
EDITED MY QUESTION PLEASE. So oh god!!! now my system is slowing down because of that limit??? (only for Clearing the Cache Process apparently) @_@Bursar
@Unpaid Oh... OK, a) global variables keep their references until the script is terminated (obviously), but local variables get unreferenced the moment you leave the local scope. If you are not using global variables you don't need unset(), because you have only the variables set, that you need (Thats more a rule of thumb and of course not a kind of law or such) b) gc_collect_cycles() only triggers the cyclic-reference-GC, not the regular one, but you don't need to care anyway, because that one runs very often ;)Curhan
@Curhan Yeh sorry I totally misread what you were saying, I thought you were talking in the scope of the script's execution but you were talking about the scope of a given line of code, which makes more sense. My impression is that gc_collect_cycles() just forces the GC to run completely though, rather than only partially. The whole global vars thing is sort of a moot point because as we all know, global variables are evil...Unpaid
@Curhan Global variables, but also properties of object. If you use a nice MVC framework or an ORM implementation, it might well be that a lot of data is queried and many objects are created, that don't get freed, because they exist in the context of a single execute method of a controller class of the framework in which the entire script is executed. No globals, still a lot of data. But indeed, the use of globals makes this scenario more likely.Shannanshannen
memory_limit is not allocation, it is maximum allowed for script execution, for ex: for memory_limit 256mb, if 10 php script simultaneously executed each with 10mb then total memory usage is 10*10mb not 10*256mb so //If your server contains, say, 4GB of memory, only two scripts simultaneously could eat all that memory// is wrong,Orangy
B
0

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.

Burnett answered 12/10, 2012 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.