PHP 7.4: Attempting to set memory_limit < 2 MB emits a warning

Version7.4
TypeChange

PHP 7.4.20 and later emits a PHP warning when the memory_limit value is set to a value smaller than 2 MB.

Internally, PHP allocates a minimum memory chunk of 2 MB for the Zend virtual machine. If an ini_set call or a direct PHP INI directive attempts to set a memory_limit value smaller than 2 MB, PHP now rejects this value, and emits a PHP warning.

ini_set('memory_limit', '100K');
PHP Warning:  Failed to set memory limit to 102400 bytes (Current memory usage is ... bytes) in ... on line ...

PHP INI files, or direct PHP CLI invocations with a memory limit set result in the same PHP warning at startup.

php -d memory_limit=100K test.php
# php.ini file
memory_limit=100K
PHP Warning:  Failed to set memory limit to 102400 bytes (Current memory usage is ... bytes) in ... on line ...

Prior to PHP 7.3, although such calls were allowed, it was an erroneous behavior due to the Zend virtual machine using a 2 MB chunk of memory to begin with. The difference since PHP 7.4.20 and later is that PHP emits a warning, and ini_set calls return false.

Backwards Compatibility Impact

All PHP INI settings with memory_limit values smaller than 2 Megabytes (2M) result in a PHP warning. Note that setting a memory limit of 2 MB is still allowed (memory_limit=2M).

memory_limit=-1 is still accepted, and its behavior of disabling the memory limit is not changed.

Applications that accept and configure a memory_limit from user configuration values might need to validate the setting to make sure it is >= 2 MB (2M).


Implementation