The confusing thing here is that the setting looks like an integer with some special syntax, but is internally defined as a string. The string is then parsed into a separate global variable whenever the value is changed. Crucially, the result of parsing the string to an integer isn't saved back to the settings table, so when you call phpinfo()
, you see the original input, not the parsed value.
You can see this in the source:
The supported syntax is ultimately defined in zend_atol
, which:
- parses the string for a numeric value, ignoring any additional text
- looks at the last character of the string, and multiplies the preceding value if it is
g
, G
, m
, M
, k
, or K
A value with no digits at the start will be parsed as zero. When setting the global variable, this will set the memory limit to the minimum allowed, based on the constant ZEND_MM_CHUNK_SIZE
.
You can see the effect by setting the memory limit, then running a loop that quickly allocates a large amount of memory and seeing what comes out in the error message. For instance:
# Invalid string; sets to compiled minimum
php -r 'ini_set("memory_limit", "HUGO"); while(true) $a[]=$a;'
# -> PHP Fatal error: Allowed memory size of 2097152 bytes exhausted
# Number followed by a string; takes the number
php -r 'ini_set("memory_limit", "4000000 HUGO"); while(true) $a[]=$a;'
# -> PHP Fatal error: Allowed memory size of 4000000 bytes exhausted
# Number followed by a string, but ending in one of the recognised suffixes
# This finds both the number and the suffix, so is equivalent to "4M", i.e. 4MiB
php -r 'ini_set("memory_limit", "4 HUGO M"); while(true) $a[]=$a;'
# -> PHP Fatal error: Allowed memory size of 4194304 bytes exhausted
(int) 'HUGO'; // => 0
. Which starts to fail on my machine at 2MB of memory used. – Olivieromemory_limit 256M
. – Groundsillmemory_get_usage(TRUE)
andmemory_get_peak_usage(TRUE)
seem to show that memory limit is 2MB. One way to surely know is by debugging PHP in gdb asmemory_limit
property for the interpreter isn't exposed in PHP – Priddy