PHP: settings memory_limits > 1024M does not work
Asked Answered
J

5

75

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.

Jotun answered 14/2, 2012 at 11:41 Comment(2)
I'm in the same situation right now. I so understand the: "For bads reasons"!Varga
Getting changes to file php.ini to take effect is a FAQ. Where is the canonical question? Related: Where can I find php.ini?, How can I find the php.ini file used by the command line?, How can I know which 'php.ini' file is used?, and php.ini changes, but not effective on Ubuntu.Anthotaxy
R
138

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.

Reedy answered 14/2, 2012 at 11:46 Comment(6)
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)Trincomalee
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?Reedy
We can do a malloc() of 4.5G. Have I understand your answer?Trincomalee
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?Reedy
On this server, 1024M. After, it fall to 256M. On others, 16384M without problem.Trincomalee
Well it might be something that Suhosin does on that server. It's been mentioned here: #5860494Reedy
B
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.

Bike answered 1/11, 2015 at 17:44 Comment(3)
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.Suggestive
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.Pushball
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.Quinquagesima
G
31
ini_set("memory_limit",-1);

This should normally remove limits

Galba answered 15/3, 2016 at 0:31 Comment(7)
setting a negative one value (-1) means it can take as much space depending on the actual memory capacity of the machine usedSnaffle
This is a very bad practice in a real production environment.Parachute
@walther, why do you say that ? Does it work or not ?Galba
The limit exists for a good reason. If you want a longer answer, here's a quick one: https://mcmap.net/q/270376/-allowed-memory-size-of-x-bytes-exhausted . 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.Parachute
Well, the bottleneck might be to read a 8Go file with more than 10 million lines with SplFileObject and LimitIterator (true story)Birdwell
@Kaymaz: So you read it as a stream, not all in one giant chunk.Bellanca
"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.Encincture
A
9

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

Antineutrino answered 1/4, 2020 at 9:45 Comment(1)
To clarify, see my comment to nxasdf's answer.Quinquagesima
R
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.

Roadside answered 31/5, 2013 at 10:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.