PHP Optimization – Benefits of Setting Lower memory_limit

htmloptimizationPHP

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

Best Answer

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.