Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

ini_set('memory_limit') changes nothing in PHP

Errorini_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.

  • Applies to PHP 8.3, PHP 8.4, PHP 8.5, Apache 2.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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());
  2. 2Match what you see, then jump to its fix

    1. 1ini_get() still shows the old limit right after ini_set()The setting is locked with php_admin_valueFix
    2. 2A 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. 3php -i shows 512M, but the failing web request shows the old valueYou edited a php.ini that the web server does not readFix

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/null

2Fix

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 512M

3Verify

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.php

Confirmed by php.net

Did this fix it?

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 = 256M

3Verify

Restart PHP and read the value back from a web request.

sudo systemctl restart php8.4-fpm

Confirmed by php.net

Did this fix it?

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 = 512M

3Verify

Read the value in a web request again.

curl -s https://example.com/info-memory.php

Confirmed by php.net

Did this fix it?

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

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.