ini_set('memory_limit') changes nothing in PHP
ini_get('memory_limit') still returns the old value after ini_set()Short answer
ini_set() only changes the value for the running request, and only when nothing above it has locked or capped the setting. Print ini_get('memory_limit') inside the failing script. If it is unchanged, look for a php_admin_value lock, a max_memory_limit cap on PHP 8.5, or a web SAPI that reads a different php.ini than your CLI.
Fix it fast
1Find out which cause you have
Print the value from inside the failing request, before and after the call, and the config file that request loaded. Do this in the web request, not only on the command line.
<?php var_dump(ini_get('memory_limit')); var_dump(ini_set('memory_limit', '512M')); var_dump(ini_get('memory_limit')); var_dump(php_ini_loaded_file(), php_sapi_name());2Match what you see, then jump to its fix
- 1
ini_get() still shows the old limit right after ini_set()The setting is locked with php_admin_valueFix - 2
A warning when you set a limit above the cap, and a lower value than you asked forThe value is above max_memory_limit (PHP 8.5)Fix - 3
php -i shows 512M, but the failing web request shows the old valueYou edited a php.ini that the web server does not readFix
- 1
What does the error look like?
ini_get('memory_limit') still returns the old value after ini_set()Warning from PHP when memory_limit is set above max_memory_limit
Cause 1 of 3
The setting is locked with php_admin_value
1Check
The manual states that a value set with php_admin_value or php_admin_flag cannot be overridden by .htaccess files or by ini_set(). Search the web server and PHP configuration for it.
grep -rn 'memory_limit' /etc/apache2 /etc/php 2>/dev/null2Fix
Change the value where the lock is set, using the same directive, and reload. If you want scripts to be allowed to raise the limit, use php_value there instead.
# Apache virtual host
php_admin_value memory_limit 512M3Verify
Reload Apache and print ini_get('memory_limit') in a web request.
sudo apachectl configtest && sudo systemctl reload apache2
curl -s https://example.com/info-memory.phpConfirmed by php.net
Cause 2 of 3
The value is above max_memory_limit (PHP 8.5)
1Check
The manual lists max_memory_limit as a startup-only setting added in PHP 8.5.0. If memory_limit is set above it, PHP emits a warning and caps the value. Its default is -1, so this only bites when someone set it.
php -i | grep -E 'max_memory_limit|^memory_limit'2Fix
Ask for a value at or below the cap, or raise max_memory_limit in php.ini, which is the only place it can be changed.
; php.ini
max_memory_limit = 1G
memory_limit = 256M3Verify
Restart PHP and read the value back from a web request.
sudo systemctl restart php8.4-fpmConfirmed by php.net
Cause 3 of 3
You edited a php.ini that the web server does not read
1Check
PHP looks for php-SAPI.ini and php.ini in a set order, and the command line and the web SAPI usually load files from different directories. Print the loaded file from a web request and compare.
php --ini
# in a web request:
var_dump(php_ini_loaded_file(), php_ini_scanned_files());2Fix
Edit the file the web request reports (or a scanned .ini in its conf.d directory), then restart the web server or PHP-FPM service.
; in the file reported by the web request
memory_limit = 512M3Verify
Read the value in a web request again.
curl -s https://example.com/info-memory.phpConfirmed by php.net
How do I stop it coming back?
- Set the limit once, in the config layer you control, instead of sprinkling ini_set() calls through code.
- Add a tiny diagnostics script, protected by IP, that prints the loaded ini file and key limits for the web SAPI.
- Fix the memory use itself (large arrays, whole files read into memory) rather than raising the limit each time.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
memory_limit does not change inside a PHP Docker container
memory_limit is still 128M after editing php.ini in the container
Your setting is being read from the wrong place or overridden.
4 causes, each with a fixBack end
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted
The script used more memory than memory_limit allows.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fix