73

For bad reasons I need to set memory_limits higher than 1 GB for a directory, but on my PHP 5.2.17 on a Debian 5.0 (Lenny) server when I use, for example, 2048M, I get only the php.ini default value (256M).

PHP runs as an Apache module, and phpinfo gives us (for the directory):

memory_limit    1024M    256M
suhosin.memory_limit    0    0

Is there a limitation due to an Apache module, or the PHP configuration? I know the server only has 4 GB of RAM. It's just a special script.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cédric Girard
  • 3,358
  • 7
  • 37
  • 52
  • 3
    I'm in the same situation right now. I so understand the: "For bads reasons"! – Simon Arnold Oct 03 '14 at 14:16
  • Getting changes to file *php.ini* to take effect is a FAQ. Where is the canonical question? Related: *[Where can I find php.ini?](https://stackoverflow.com/questions/8684609/)*, *[How can I find the php.ini file used by the command line?](https://stackoverflow.com/questions/2750580)*, *[How can I know which 'php.ini' file is used?](https://stackoverflow.com/questions/14558150)*, and *[php.ini changes, but not effective on Ubuntu](https://stackoverflow.com/questions/11662295)*. – Peter Mortensen Oct 26 '21 at 17:12

5 Answers5

135

How are you trying to set the memory limit? phpinfo() shows current PHP reserved memory limit, and this is what is available due to php.ini having that set as a memory limit.

Writing this to the Apache .htaccess file in your script directory might work if your server supports setting PHP commands through .htaccess:

php_value memory_limit 2048M

Since it may be possible that .htaccess commands for setting PHP values are turned off. Then you can also try this from PHP code:

ini_set('memory_limit', '2048M');

If this doesn't work and .htaccess also doesn't work, then you need to contact the server administrators.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kingmaple
  • 4,200
  • 5
  • 32
  • 44
  • 2
    I use php_value or ini_set, same problem : if I set 512M ou 1024M, it's ok, if I set 1500M or 2048M, I get 256Mo (the default value from php.ini) – Cédric Girard Feb 14 '12 at 12:04
  • 1
    In this case it seems to be out of PHP's hands, what is the maximum value that you can allocate or is 1024M max? – kingmaple Feb 14 '12 at 12:16
  • We can do a malloc() of 4.5G. Have I understand your answer? – Cédric Girard Feb 14 '12 at 12:53
  • 1
    No I mean, based on your own tests of setting memory with .htaccess or ini_set(), what is the highest value that 'sticks' that you have tried? – kingmaple Feb 14 '12 at 14:27
  • On this server, 1024M. After, it fall to 256M. On others, 16384M without problem. – Cédric Girard Feb 15 '12 at 08:34
  • 1
    Well it might be something that Suhosin does on that server. It's been mentioned here: http://stackoverflow.com/questions/5859994/ini-setmemory-limit-doesnt-work-and-returns-false-cant-figure-out-wh – kingmaple Feb 15 '12 at 11:15
38

I had this same issue where I needed my PHP script to use 4 GBs of RAM. The reason doesn't matter. The goal was to set a 4 GB limit in PHP.

The initial idea was to use ini_set('memory_limit', '4096M');, but I found that this didn't work. I have no idea how or why to be honest, but that wasn't important to me at the time. I'm on a system with 32 GBs of RAM, and it has to be possible.

I found that setting a limit 1 MB less actually worked, and it's a solution that worked for me.

ini_set('memory_limit', '4095M'); // 4 GBs minus 1 MB

Actually, I've never really played with it before, but I've noticed on my system that this value seems to completely remove all memory limits for me. I'm able to use as much RAM as my system possibly provides.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nxasdf
  • 1,088
  • 1
  • 11
  • 11
  • did you get why 4096mb isn't working? It happened with me on php 7. this "bug" (idk if it is bug) didn't show any thing on page neither error. I have put also error_reporting. – Phoenix404 Feb 24 '18 at 08:52
  • I also just experienced this bug with php 7.1. As soon as I set it to 4095M, it started working again. Assuming this is for CLI usage, perhaps it would be better to just set it to -1 in instances where you need greater than 4GB of memory. – rtd1123 Jul 13 '18 at 15:25
  • 1
    The reason for releasing memory_limit by this value is that the value should be integer, which has a limit to 2^31 (2G) on 32b PHP version, no matter if you run it on 64b OS. Anything larger is stored as a float (IEEE754 format) and re-interpreted as integer (LE or BE, depending on the architecture). If it happens to be lower than zero, it releases the limit. One way or another it results to undefined behavior. – Jan Turoň Sep 21 '20 at 10:35
30
ini_set("memory_limit",-1);

This should normally remove limits

pollux1er
  • 5,372
  • 5
  • 37
  • 36
  • 9
    setting a negative one value (-1) means it can take as much space depending on the actual memory capacity of the machine used – Abel Callejo Dec 09 '16 at 05:31
  • 23
    This is a very bad practice in a real production environment. – walther Aug 10 '17 at 09:15
  • @walther, why do you say that ? Does it work or not ? – pollux1er Aug 11 '17 at 09:35
  • 13
    The limit exists for a good reason. If you want a longer answer, here's a quick one: https://stackoverflow.com/a/10151831/1289283 . Anyway, as a programmer your job is to not only "make it work", but use your resources as effective as possible. Removing limits in code like this is never a good practice. A good practice is to find the bottleneck and fix it. – walther Aug 11 '17 at 10:26
  • Well, the bottleneck might be to read a 8Go file with more than 10 million lines with `SplFileObject` and `LimitIterator` (true story) – Kaymaz Apr 25 '18 at 14:21
  • 1
    @Kaymaz: So you read it as a stream, not all in one giant chunk. – Ryan Parman Jun 08 '18 at 22:02
  • 6
    "what could go wrong?" - the next night your customer calls you because his server died. Turns out, a script with removed limits was invoked a few hundred times in parallel, triggering the kernels OOM killer, which killed MySQL, SSH and a few other important services. Limits are there for a good reason. – Kaii Mar 05 '19 at 14:55
8

The limit of

ini_set('memory_limit', '4095M'); // 4 GBs minus 1 MB

is related to the operating system (OS). With a 32-bit OS you must use this maximum limit.

I resolved an issue with memory_limit today using this workaround.

Using a higher limit, PHP has strange behaviour. It returns exhausted memory in some public non-static method class when a class was instanced, but putting a die() in the __construct method (to test the problem).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
6

As described in Hardened-PHP - Configuration, Suhosin will not let you set a value greater than the one the script started with.

If you set the suhosin.memory_limit to 2048M then you'll be able to increase your memory usage.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131