memory_limit is just as a highway speed limit is per-vehicle. And a highway can have an infinite amount of lanes (parallel processes), and an infinite amount of cars. PHP Memory Limit is the maximum amount of memory one PHP process is allowed to use. One PHP process usually means a specific page view.
Memory Limit is Not The Same as RAM
While RAM is the total available memory, the memory limit is per PHP process. That means that your site can consume e.g 10 GB of RAM, with a memory limit of 256 MB. We do not limit the amount of RAM you can use, just the memory limit.
How to Work With Memory Limits
Though it may seem like a good solution to just max the memory limit, it’s not a wise decision. Memory is one of the easiest things to misuse, due to bugs, poorly written code etc. You can easily, with a few lines of poorly written PHP, i.e a never ending loop, use multiple gigabytes of memory per pageview. Let’s say you spend 1 GB of memory per pageview, that means 1 GB per visitor to your website. Add a few hundred visitors to your website, and you will be spending hundreds of gigabyte of memory.
That’s why you should always set the memory limit to as low as possible, as this setting actively help you scale. Having the memory limit as low as possible will also ensure that if you have a bug in your system it won’t affect the scalability of your site as much as if you maximised the memory limit.
Max PHP Memory Limit on Servebolt
Your website will have a max limit of 1024 MB (1 GB) when accessed through the webserver over http or https, and when you reach that limit, it’s time to start working on your application to fix the non-scalable issues with it.
Via the Command Line (CLI), you can use a PHP memory Limit of 2560 MB. However, depending on your site and code, this limit can be lowered. The PHP memory Limit is capped depending on your Bolt Plan. To see how much MB CLI is allowed to run on your site, run this over SSH:
php -i | grep "memory_limit"
What happens when I exceed the PHP Memory Limit?
When a page request over http or https exceeds the PHP Memory Limit, the user will likely experience the white screen of death, or receive a 500 Internal Server Error message. Such errors are logged in your PHP error log, and the message will say something like:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 44 bytes) in [filename]
The number of bytes can be converted to MB by dividing it twice by 1024. This example translates to a memory limit of 128MB.
When this happens, you have two options. You can either increase the PHP memory limit to allow for higher memory usage, or optimize your application (WordPress for example) to use less memory.
It is quite common to experience that it does not help to increase the memory limit, and that PHP will consume the full limit and still fail. If that’s the case, you have a bug in your application that exhausts memory.
Memory Exhaustion Only for Certain Pages
PHP Memory exhaustion usually happens per request. That means that it often does not apply to all pages on your website. For example, you may experience that a single admin page is producing this error, or that a large category page fails on your eCommerce site. If that’s the case, and other pages work normally, you can usually be sure that it is code related to building that specific page that needs optimization.
How does PHP Memory Limit Relate to Performance?
Increasing or reducing the PHP Memory Limit has no impact on the speed of a website. It only determines whether or not the PHP process will successfully complete and deliver the response to the visitor. The PHP Memory Limit has an effect on scalability, because the amount of RAM available always is a limited resource.
Give us your feedback on this article
Servebolt has launched Accelerated Domains!
Accelerated Domains is a service that greatly improves and optimizes your site on these four areas: Performance, Scalability, Security and the Carbon footprint of your website.