PHP memory_limit is per-script, as is highway speed limit per vehicle. For example, although PHP’s memory limit can be set as high as 1GB, it doesn’t mean that scripts will pile up to use that 1GB. let’s take a look Understanding PHP’s memory_limit Settings.
PHP memory_limit is a per-script setting
The docs for PHP.net call it like this:
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 key_buffer_size or innodb_buffer_pool settings, the PHP memory_limit setting is No A storage location where multiple PHP scripts grow from or within a pool. rather it is a per-script limit, PHP memory_limit is the maximum amount of server memory a One PHP scripts are allowed to be consumed. 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 one 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 (5) PHP scripts are simultaneously using 100MB each, there will be a total of 500MB PHP memory usage, and the PHP memory limit of 128M will not be hit.
That said, for scripts that request other PHP scripts using require, include, or include_online, this limit is then inherited and shared by all include scripts that depend on the original script.
“The include statement takes all the text/code/markup present in the specified file and copies it to the file using the include statement. The require() function is similar to include() except that it errors out differently.” If an error occurs, the include() function generates a warning, but the script will continue to execute. require() generates a fatal error, and the script will stop.” , W3School,
“The require_once() statement is identical to require() except PHP will check whether the file is already included, and if so, do not include (require) it again.” , php.net
PHP memory_limit is per-script, as is highway speed limit per vehicle.
Now about the basic example mentioned in the beginning. A lower setting of 128M is always better because if PHP scripts are trying to use more than 128M, those scripts will now return errors that exceed the memory limit. In the above issue, this was not the case, so regardless of the 128M or 1G memory_limit setting, it only comes into play if there is a disabled script.
Fortunately, the PHP memory_limit setting will block disabled code, which then alerts you to optimize your code. Until fixed, you may want to temporarily increase the PHP memory_limit so that your web application becomes unusable due to PHP out-of-memory errors.
If memory is not available on your server, sometimes you will find it difficult to decide whether to increase PHP memory_limit or optimize your code to meet the requirements of the script. It would be best if you always customize as the preferred option when possible. Also, you can increase the memory limit of PHP for specific websites. One way would be to place a php.ini file in the webroot of the site. You can also set limit for specific scriptname.php. for example using ini_set(‘memory_limit’,’256MB’),
How to Extend PHP memory_limit
To increase the PHP memory limit setting, edit your PHP.ini file. Increase the default value of PHP (Example: Maximum amount of memory a script can consume = 128MB) memory limit line in php.ini.
memory_limit = 256M
Alternatively, you can edit your .htaccess file (not recommended. See: apache display:disable .htaccess)
php_value memory_limit 256M
If you do not have access to these files or lack the experience to make this change, you can contact your web host and ask them to increase your PHP memory limit.
Originally published: September 18, 2017.
Last Updated: 19 August 2021