PHP’s memory_limit is per-script, just as a highway’s speed limit is per-vehicle. For example, although PHP’s memory limit may be set to 1GB, that does not mean that scripts will pile up to use that 1 GB. Let’s take a closer look at understanding PHP’s memory_limit setting.
PHP memory_limit is a per-script setting
PHP.net’s documentation puts it this way:
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...
Unlike, say, MySQL’s innodb_buffer_pool settings, the PHP memory_limit setting is not a storage space where multiple PHP scripts pool from or grow within. Rather, it’s a per-script limit. PHP memory_limit is the maximum amount of server memory a single PHP script is allowed to consume. When blocked, the resulting error output looks something like this:
Fatal error: Allowed memory size of x bytes exhausted (tried to allocate x bytes) in /path/to/php/script
Or like this:
PHP Fatal error: Out of memory (allocated x) (tried to allocate x bytes) in /path/to/php/script
So, for example, if two or more scripts are requested simultaneously, each is completely independent of the other. They do not share the memory_limit setting. Remember, PHP is not designed for and does not support multithreading. Thus, if five PHP scripts simultaneously use 100 MB each, that would consume 500 MB of total PHP memory usage on the server. Yet, in this scenario, a PHP memory_limit setting of ‘256M’ per script would not be hit.
That said, for scripts that request other PHP scripts inline using require, include, or include_once, this limit is then inherited and shared by all included scripts that are dependent on the parent script.
“The include statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.” – W3schools.
“The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.” – php.net
PHP memory_limit is per-script, just as a highway’s speed limit is per-vehicle.
Now, regarding the original example mentioned at the outset. A lower setting of 256M is always better because if PHP scripts are trying to use more than 256M, those scripts would now return memory limit exceeded errors. In the above issue, that was not the case, so regardless of the 256M or 1G memory_limit setting, it only comes into play if there are inefficient scripts.
Fortunately, the PHP memory_limit setting will block inefficient code, alerting you to optimize your code. Until fixed, you may want to temporarily increase PHP memory_limit to avoid your web application becoming unusable due to PHP out-of-memory errors.
If you don’t have available memory on your server, you’ll sometimes be faced with deciding whether to increase PHP memory_limit to meet the requirements of scripts or to optimize your code. It would be best if you always optimized as the preferred option when possible. Also, you can increase PHP’s memory limit for specific websites. One method would be to place a php.ini file in the site’s webroot. You can even set the limit for specific scriptname.php. For example, using ini_set(‘memory_limit’,’256MB’).
How to increase PHP memory_limit
To increase the PHP memory limit setting, edit your PHP.ini file. Increase the default value (example: Maximum amount of memory a script may consume = 256MB) of the PHP memory limit line in php.ini.
memory_limit = 2048M
Alternatively, you can edit your .htaccess file (Not recommended. See: Apache Performance: Disable .htaccess)
php_value memory_limit 2048M
If you don’t have access to these files or the experience to make this change, you can contact your web host and ask them to increase your PHP memory limit.
While your PHP configuration might boast a generous memory limit, it’s important to grasp that individual scripts don’t accumulate memory like a shared pool. Instead, each script operates within its allocated memory boundary.
This per-script nature ensures that one script’s inefficiency won’t hinder others. If multiple scripts are executed simultaneously, they remain independent and won’t collectively breach the memory limit. However, when scripts call upon other scripts using ‘require’ or ‘include,’ the parent script’s memory limit extends to its dependencies.
The PHP memory_limit setting serves as a guardian against runaway scripts, throwing ‘memory limit exceeded’ errors when needed. It’s a valuable tool for identifying and rectifying inefficient code. When facing such errors, it’s wise to temporarily increase the memory limit, allowing your web application to remain functional while you optimize your code.
In conclusion, PHP memory_limit is a crucial element in maintaining the performance and reliability of your web applications. By comprehending its per-script nature and using it judiciously, you can ensure that your websites run smoothly and efficiently, providing a seamless experience for users and developers alike.
Originally published: September 18th, 2017 | Last updated: September 22, 2023.