PHP: settings memory_limits > 1024M does not work

PhpMemory Limit

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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.

Solution 3 - Php

ini_set("memory_limit",-1);

This should normally remove limits

Solution 4 - Php

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.

Solution 5 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCédric GirardView Question on Stackoverflow
Solution 1 - PhpkingmapleView Answer on Stackoverflow
Solution 2 - PhpnxasdfView Answer on Stackoverflow
Solution 3 - Phppollux1erView Answer on Stackoverflow
Solution 4 - PhpVincent TouzetView Answer on Stackoverflow
Solution 5 - PhpStefano TurinettiView Answer on Stackoverflow