What is PHP memory limit?

PHP 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 is usually a 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 limit

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.

Give us your feedback on this article