5

I need to execute just few lines of PHP code on every single page of my website. This PHP code does not require more than 4MB memory. That is why i want to allocate just 4MB memory to all pages by using following code :

ini_set('memory_limit','4M');

Does it give any benfits in any of the following areas :
1. Website speed
2. Bandwidth optimization
3. Optimizing server memory usage

1 Answer 1

9

It won't give you any speed boost, nor bandwidth optimization, nor server memory usage.

What memory limit says is that when a script tries to use more memory, a error occurs. This is the only effect. PHP won't magically optimize memory if you reach the memory limit. If an object takes 16 bytes of memory, it will always take 16 bytes, no matter what is the actual memory limit and if it is reached or about to be reached.

  1. Website speed depends on many factors such as the size of HTML, CSS, JavaScript and images, the number of requests per page required to load all the content, the time it takes to the code-behind to process the input and generate the output, etc. Memory limit has no effect on this.

  2. Bandwidth usage depends on the size of HTML, CSS, JavaScript and images, the number of requests per page required to load all the content. Setting 2 MB or 200 MB as a limit won't change anything.

  3. Memory usage for a given script won't change neither, since it depends on the number of objects and their respective size. If and only if the different scripts are randomly taking too much memory and the only response you have is to kill them, then it will have a positive impact on memory usage: instead of filling the memory, those bogus scripts will simply be stopped by a PHP error.

    I highly doubt such case exists. When a script takes too much memory, what should a developer do is to inspect it with a profiler and to fix the parts which are allocating too much memory. Killing the instances which fill up memory is the weirdest way to "solve" the problem.

So why would anyone use the memory limit?

The memory limit can be useful in debug, to be sure that your application doesn't start to take too much memory.

This doesn't replace a profiler, but can be a good solution where you don't want so use more complex tools, but still want to be alerted when, one day, your app grows enough to fill 2, 50 or 200 MB of memory.

Note: PHP documentation is very clear on that:

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server.

Source: Description of core php.ini directives, emphasis mine.

6
  • What about when website is having too many visitors at a same time. Is there any chance that some visitor have to wait(for microseconds of delay) to have memory allocated for the execution? Commented Aug 12, 2013 at 11:36
  • @Forte: yes, they'll wait for the memory allocation. Setting low memory limit would throw away clients who are using too much memory, enabling other clients to use the website faster. Are there any cases where it is acceptable to throw a client away because the app is consuming too much memory? I don't think so. Commented Aug 12, 2013 at 11:48
  • My app will never consume more than 2MB, that is for sure. That is why i have decreased allowed memory limit so that server can utilize un-used memory for other visitor reqeusts. Commented Aug 12, 2013 at 11:55
  • 1
    @Forte: if your app never consumes more than 2 MB, setting the memory limit to 4 MB or 400 MB won't make any difference. Commented Aug 12, 2013 at 12:12
  • 3
    @Forte you seem to make the (incorrect) assumption that PHP will allocate the set memory_limit for each running script. That is not true. The manuals states: "This sets the maximum amount of memory in bytes that a script is allowed to allocate.". Memory gets allocated when necessary, not earlier.
    – CodeCaster
    Commented Aug 12, 2013 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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