PHP Memory limit not taking effect in normal php scripts

Go To


I've assigned the memory limit of php to 999m so it appears in phpinfo like

memory_limit 999M 999M

when I use phpinfo(); to show it.

Unfortunately when I try to run a fairly large script, it seems like the limit is 256M

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 40 bytes) in /xxx/wp-includes/wp-db.php on line 1403

Anyone have any ideas why or what I can do to increase the limit (and have it actually work)

If it helps I'm running centos5 32bit and with php running in fcgi mode

2012-04-03 20:31
by Darrell Ding
You edited the correct php.ini? (See phpinfo(), or php -i (note, that the different sapis (apache-module, cgi, cli, ...) use different php.inis)) You restarted your server? You ever thought about, why your scripts requires sooo much memory? - KingCrunch 2012-04-03 20:33
memory_limit 999M 999M - surely you meant memory_limit 999Md_inevitable 2012-04-03 20:33
isn't your WordPress installation setting its own memory limit via ini_set('memory_limit', ...) - kuba 2012-04-03 20:35
how about optimising the script, you shouldn't need that much memory - NoName 2012-04-03 20:36
Yeah I did edit the correct php.ini, it shows fine in phpinf - Darrell Ding 2012-04-03 20:37
@dinevitable Yeah that was the output from phpinfo in php.ini its set to memorylimit 999 - Darrell Ding 2012-04-03 20:38
@Dagon That's not an option, its wordpress, I can't optimize it since I have truckload of post metas used - Darrell Ding 2012-04-03 20:39
that's just a poor excuse for writing bad code - NoName 2012-04-03 20:40
I don't know what version of php your using, but prior to PHP 5.2.1, in order to use this directive it had to be enabled at compile time by using --enable-memory-limit in the configure line. (From the php manual - ArendE 2012-04-03 20:45
Is this VPS or shared host? If so perhaps that is the physical limit or the upper limit set by your provider - d_inevitable 2012-04-03 20:54
I did not see if you were running php-fpm. But if you are/were then you should check your fpm pool to see if it is set there. I ran into this where the pool file had php_admin_value[memory_limit]=32Mjohn 2017-06-13 09:56


Create a php file called test.php, put inside:


Check for "Configuration File (php.ini) Path" and "Loaded Configuration File" to see the correct php.ini path. Edit php.ini and search for memory_limit and set it to:

memory_limit = 999M

Check if you have more than one occurrency of memory_limit into the php.ini file. In the case, delete it.

Stop apache, and then restart it (apachectl restart | apachectl graceful | kill -1 are not ok. Stop, then start).

Recheck test.php to see if the new parameter got acquired.

2012-04-03 21:03
by dAm2K
The parameter is acquired, just doesn't work in wordpress, there isn't any overrides, did a grep with for ini_set and nad - Darrell Ding 2012-04-03 21:16
@DarrellDing you should grep php_value if you want to see if it's set in apache somewhere as well - craniumonempty 2012-04-03 21:21
My wordpress version (3.1.2) defines a file called wp-includes/default-constants.php that create a constant called WPMEMORYLIMIT. You can ovveride its value into wp-settings.php. Wordpress overwrite the php memory limit value setted into php.ini if it's smaller than the value you define with WPMEMORYLIMIT - dAm2K 2012-04-03 21:36
Fixed, it wasn't WPMEMORYLIMIT, apparently wordpress uses WPMAXMEMORY_LIMIT as their admin max limit - Darrell Ding 2012-04-03 21:40
Was the closest I got to an answe - Darrell Ding 2012-04-03 22:09


I had a similar issue, for me it was an extra ini file that was loaded called "99-liip-developer.ini". At the top of the file the memory_limit was defined at 265M, which overwrote the memory_limit defined in php.ini.

Hope this helps anyone.

2013-08-27 10:00
by Barry Kooij
Found a file called www.conf in /etc/nginx/fpm/pool.d. I would have never even thought to look there - serialworm 2013-12-12 15:47
Thank you! :) This was driving me craz - Adam Elsodaney 2014-07-26 20:30


I suggest you set this on the top of your script:


in the script that is consuming so much of your memory instead of allowing all scripts to consume so much memory. You can also put this in the .htaccess of your /wp-includes/

 php_value memory_limit 512M

More information and explanation here:

2012-04-03 20:40
by ArendE
Its already been set under /etc/php.ini so the limit is high enough, just wondering why it doesn't take effect in the script but the limit is shown under a phpinf - Darrell Ding 2012-04-03 20:45
Because it can be overruled by wordpress in the wp-config.php file - ArendE 2012-04-03 20:47
By using define('WP_MEMORY_LIMIT', '64M'); in that file for example - ArendE 2012-04-03 20:49
Its not, already has both iniset('memorylimit', '999M'); define('WPMEMORYLIMIT', '999M'); defined inside - Darrell Ding 2012-04-03 20:52
Well, it's very simple, there is still a limit somewhere. This could be set by your webhost, in some file, .htaccess etc. I suggest you try using all options to override the memory_limit without asking why it won't work as you want it or should be. Also look at the same question here maybe it could be of some use : - ArendE 2012-04-03 21:02


You can try putting this at the top of your file

ini_set('memory_limit', '999M')

How much system memory do you have available? Did you restart Apache after editing your php.ini file (I assume you have php installed as a module)?


sudo /etc/init.d/httpd restart
2012-04-03 20:40
by Matthew Blancarte
I've already tried that it still maxes out at 256 for some vague reaso - Darrell Ding 2012-04-03 20:44
Did you restart apache? The code in your php.ini was invalid, too... Are you sure you don't have a second setting in your php.ini that is overwriting your custom line? Check for that, too.. - Matthew Blancarte 2012-04-03 20:47
it shows memory limit 999m in phpinfo so that isn't the issue right now, I'm just wondering how come it doesn't work in wordpress. Wordpress's wp-config has been set to use up to 999 as wel - Darrell Ding 2012-04-03 20:54
Did... you... restart... apache - Matthew Blancarte 2012-04-03 20:59
Who manages your server? How much memory is currently available if you run "free -m" via the command line - Matthew Blancarte 2012-04-03 21:01
total used free shared buffers cached Mem: 2025 1925 99 0 25 1381 -/+ buffers/cache: 518 1506 Swap: 4094 369 372 - Darrell Ding 2012-04-03 21:03
well, time to reinstall php lol... Seriously, though, this is quite odd. Try restarting apache again and see if it takes hold. Very very weird.. - Matthew Blancarte 2012-04-03 21:07


Unsure, but this came up in the google search for me:

The ping optimizer was cramming a table full on the DB and clearing solved the problem. The error was almost the same:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 80 bytes) in /home/user1/public_html/domain/wp-includes/wp-db.php on line 1400

So see if a plugin is doing the same, then clear it and log it on WP somewhere so it can be fixed if that is the problem.

2012-04-03 20:55
by craniumonempty
My problem is that I'm unable to increase the memory limit no matter what I do, not so much that I'm having an erro - Darrell Ding 2012-04-03 20:58
@DarrellDing are you using any plugins like Memory Bump? You might want to check the setting there as well. Look all the way down the line: php.ini (and local php.ini if there are multiple), .htaccess, and search the php files for changes as well. Check with a small script to the side (just a quick script not in wordpress) to see if the php.ini and/or .htaccess files set it properly using ini_get('memory_limit') to check it - craniumonempty 2012-04-03 21:09


Make sure you server/virtual server in apache is not configured to overwrite PHP configuration. Even if you use:

php_value memory_limit 512M

Your server may have something like:

php_value memory_limit 32M

which will make your changes in php.ini useless. To fix this:

  • Either edit the php_value value in your server configuration.
  • Or remove it from your web server, so it uses PHP global settings.
2013-10-21 09:35
by Benjamin Piette