Description
Description
I'd like a max_memory_limit
(naming?) ini setting to ensure that PHP never raises the memory_limit
ini setting higher than a certain point. This setting should not be editable using ini_set
. I'd be open to placing the max_memory_limit
setting somewhere else.
Example scenario:
Running a PHP application in Kubernetes you'd have set some resource constraints. Let's say you've allocated 256 MB to a pod running the application and set memory_limit
to 128 MB in php.ini
.
This setup means we'll have 128 MB available in PHP and a buffer of 128 MB for the rest of the pod. (Assuming only one PHP script will be executed simultaneously, since memory_limit
is per script)
Now to the issue. PHP has support for changing the memory_limit
setting using the ini_set
function during runtime.
If the memory_limit
is set to a value higher than the total allocated memory in the pod (256 MB in this scenario), then when the pod's memory of 256 MB has been exhausted by PHP, it will be the kernel OOM killer that handles the exhaustion instead of PHP. The OOM killer then sends PHP a SIGKILL signal which causes a non-graceful exit. (Meaning that potential shutdown handler(s) registered using register_shutdown_function()
won't get to run)
Suggested behaviour
php.ini
:
memory_limit = "128M"
max_memory_limit = "256M"
PHP code
:
<?php
echo ini_get('memory_limit'); // Output: 128M
ini_set('memory_limit', '256M');
echo ini_get('memory_limit'); // Output: 256M
ini_set('memory_limit', '512M'); // Maybe this should cause a PHP notice?
echo ini_get('memory_limit'); // Output: 256M
// Attempt at working around by setting max_memory_limit
ini_set('max_memory_limit', '512M'); // Should have no effect. Maybe this should cause a PHP notice/warning/fatal?
ini_set('memory_limit', '512M'); // Maybe this should cause a PHP notice?
echo ini_get('memory_limit'); // Output: 256M