api to set memory_limit to "at least" this much
Description
it's not rare that i see code like ini_set('memory_limit', '1G'); , where the intention is not really to set the memory limit to 1G, but to make sure that the limit is at least 1G, if some other code in the same script requires 4G, i don't want to overwrite that.. would be nice if there was some api kindof like
function memory_limit_set_at_least(int $at_least): void
{
$current = ini_get("memory_limit");
if ($current === "-1") {
// it's already unlimited.
return;
}
$modifier = $current[strlen($current) - 1];
$current = (int) $current;
if ($modifier === "K" || $modifier === "k") {
$current *= 1024;
} elseif ($modifier === "M" || $modifier === "m") {
$current *= 1024 * 1024;
} elseif ($modifier === "G" || $modifier === "g") {
$current *= 1024 * 1024 * 1024;
} else {
// there is no modifier.
}
if ($current < $at_least) {
ini_set("memory_limit", (string) $at_least);
}
}
Wouldn't it be better, if there was a way to retrieve the currently set memory limit as number? At least, that appears to be generally more useful (e.g. to compare to memory_get_usage()).
Have to be with \Closure or something? Otherwise new limit will stay until process dies. (c) Workers never dies.
How about language construct like "allocate" that dissapears like variable scope??
Also javascript (that never dies too) solves overflow with recursion limit (php hasn't it). Yes there could be overflow if you get from database today 1000 records and 100000 in next month... but 70+% overflows is happened cause of infinite recursion (that you prefer to debug with stack trace but you never know where limit comes - "tried to allocate blah blah" "somewhere"
if there was a way to retrieve the currently set memory limit as number?
that's a good idea, the code above would be much simpler if such a function existed, and php certainly has that raw number stored somewhere ^^
wonder how "unlimited" would be represented in such a function tho, should it return -1 for no limit? or null? or +INF ? (if it returns +INF then the return would be int|float ), and how should 32bit systems report a limit above 2GB? float maybe?
wonder how "unlimited" would be represented in such a function tho, should it return -1 for no limit? or null?
PHP uses int|null or int|false in similar cases.
Wouldn't it be better, if there was a way to retrieve the currently set memory limit as number?
This will help OP, but I wonder if there is a chance to add callback for when ini settings are changed, this would allow to control what libraries are doing and in this case control memory limit. ini_changed(string $name, callback $value); with $value = fn($old_value, $new_value)=>$new_value.
Also ini_set supposed to return false on failure, but if you try setting ridiculous value like ini_set('memory_limit', '2K') you'll get old value, and ini_get will return 2K, but limit remains unchanged - limit cannot be lower than currently allocated.
wonder how "unlimited" would be represented in such a function tho, […]
There is no "unlimited"; that would just be a very large value, never reached in practise. The memory_limit is internally stored as zend_long and size_t.
This will help OP, but I wonder if there is a chance to add callback for when ini settings are changed, […]
That may be a good idea, or maybe not.
Anyhow, what to implement exactly obviously requires discussion, and warrants an RFC.
Also ini_set supposed to return false on failure, but if you try setting ridiculous value like ini_set('memory_limit', '2K') you'll get old value, […]
That is supposed to be fixed as of PHP 8.1.0: https://3v4l.org/psYLo
There is no "unlimited"
memiry_limit=-1 means unlimited today (i guess it's internally stored as SIZE_T_MAX or something then)
(Accidentally pressed "comment and close" instead of just comment)
Once you set unlimited you EXACTLY meet what happens when you worker takes all 100gib of hard drive in 2 minutes if something went wrong. Short forecast: production falls down, you'll be fired, cause of you're the guy that DOES something, instead of other "managers" that SPEAK something.
Created a small implementation for memory_get_limit(): https://github.com/php/php-src/compare/master...iluuu1994:memory-get-limit. It is very simple. memory_reset_peak_usage() was added without an RFC. I wonder if an RFC for this is really necessary or if a mail to the mailing list is enough.
@iluuu1994, nice! I suggest to create a PR to perhaps get some feedback first. Could then write to internals, and if controversial, could still pursue the RFC process.
@cmb69 Done: https://github.com/php/php-src/pull/8380
Let's close this since https://github.com/php/php-src/pull/8454 was merged.