Set Max Memory Limit Value in PHP 8.5

Set Max Memory Limit Value in PHP 8.5

PHP provides the memory_limit directive which specifies the maximum amount of memory a script is allowed to allocate. When the limit is exceeded, the execution stops immediately. The memory_limit can be set in the configuration file (php.ini) or adjusted at runtime using the ini_set function. When set to -1, it means that there is no memory limit.

Since PHP 8.5, we can use the max_memory_limit directive, which specifies the maximum memory_limit value that may be configured at startup or runtime. By default, the max_memory_limit is set to -1 which means this limit is not enabled.

The max_memory_limit can be set in the configuration file (php.ini) and cannot be changed at runtime. When max_memory_limit is defined, it becomes the maximum value that memory_limit is allowed to use. Any attempt to set a higher limit triggers a warning, and memory_limit is automatically limited to the max_memory_limit value instead.

php.ini

memory_limit = 128M
max_memory_limit = 512M

main.php

<?php

ini_set('memory_limit', '256M'); // OK, lower than max_memory_limit
ini_set('memory_limit', '512M'); // OK, equal to max_memory_limit
ini_set('memory_limit', '1024M'); // Warning: Failed to set memory_limit to ...

Leave a Comment

Cancel reply

Your email address will not be published.