64

I have an problem increasing memory limit for PHP as Apache module.

If I put following directive in Apache configuration, that work OK:

php_value memory_limit 1.99G

But over 2GB do not work, it's restore this value back to 128MB.

What is the problem here? I need more memory for some PDF related tasks.

Server is Debian 2.6.32-5-amd64 #1 SMP, PHP 5.3.3-7+squeeze13 with 12GB physical RAM.

mikikg
  • 1,488
  • 1
  • 11
  • 23
  • 10
    2G apache processes! Wow. – Ray Aug 09 '12 at 14:05
  • Maybe you should leave the apache configuration as default and `ini_set('memory_limit', -1);` only on the file you need it. See if that works. – JRomero Aug 09 '12 at 14:07
  • Using MB instead GB do not change anything. I thing there is some other limit maybe somewhere in Apache configuration. Also, as I know memory_limit can not be changed in run-time and default value is 128MB. Maybe I can set this in main php.ini but I do not want other VHOST to have such large settings so I use it per VHOST. – mikikg Aug 09 '12 at 14:12
  • @mikikg you can't tie php settings to a vitual host. Once an apache process has finished serving a request it's free to be used in any other request across any of the virtual hosts. – Ray Aug 09 '12 at 14:20
  • @Ray hmm, do not understand this explanation? PHP settings can be changed per virtual hosts via apache configuration, I'm already using such things for various purpose. – mikikg Aug 09 '12 at 14:31
  • @mikikg once your script uses the memory, until the apache process is reaped it will keep hold of that much memory. You could set max requests to '1' and after every request the child process would die, but this is a apache server setting and I wouldn't suggest doing it. – Ray Aug 09 '12 at 14:34
  • @Ray Ok, understand that. The trick is in that this script was working in some older Debian server where I increase memory on same way over 2GB, forget what exactly because older server "gone with the flow" (RAID0 crashed!) – mikikg Aug 09 '12 at 14:41
  • At least, can you advice me some good shell based HTML2PDF converter? – mikikg Aug 09 '12 at 14:53
  • 1
    I have the same problem even on commandline php and with `ini_set("memory_limit", "-1")` so I am sure that limitation is in php and not in apache. Maybe it hast todo with the 32bit max_int – Radon8472 Feb 04 '20 at 21:52

8 Answers8

66

Have you tried using the value in MB ?

php_value memory_limit 2048M

Also try editing this value in php.ini not Apache.

Amit Bera
  • 7,581
  • 7
  • 31
  • 57
Claudiu Claw
  • 893
  • 7
  • 10
57

I would suggest you are looking at the problem in the wrong light. The questtion should be 'what am i doing that needs 2G memory inside a apache process with Php via apache module and is this tool set best suited for the job?'

Yes you can strap a rocket onto a ford pinto, but it's probably not the right solution.

Regardless, I'll provide the rocket if you really need it... you can add to the top of the script.

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

This will set it for just the script. You will still need to tell apache to allow that much for a php script (I think).

Ray
  • 40,256
  • 21
  • 101
  • 138
  • 2
    I know, script is not mine, it is some html2pdf converter and input to it is very large HTML page. – mikikg Aug 09 '12 at 14:16
  • Right solution and right explanation! I ran into the memory limit for just 1 script, my sitemap, which was indexing too many pages (which were stored in the DB). Perfect solution, sitemap has a high memory limit, everything else remains untouched. – HoldOffHunger Jul 08 '17 at 01:13
  • 1
    Should go without saying, but make sure you pump the tyres right up before you strap the rocket on. – ban-geoengineering Oct 04 '18 at 13:10
  • 2
    sometimes u need a rocket – Andrew Nov 02 '18 at 19:11
  • 1
    I was in dilemma since last 7 hours over the required value needed for my site. All QA's are blocked, my project manager was frustated and i am also tensed. I could've used the rocket luckily i came here and this analogy made me laugh so hard that i forgot why i came here . BTW i guess we should always start with why instead of how. – Pawan Saxena Dec 11 '20 at 18:19
26

For unlimited memory limit set -1 in memory_limit variable:

ini_set('memory_limit', '-1');
scrowler
  • 24,273
  • 9
  • 60
  • 92
Indrajeet Singh
  • 2,958
  • 25
  • 25
  • 5
    It's nice to know that unlimited memory is possible, but, I don't think I'll be using this in my code soon. =) – HoldOffHunger Jul 08 '17 at 01:15
  • 3
    This thing works but its very dangerous to use on production servers as it might put your entire server down due to high memory consumption due to poor garbage collection in your code. – chintan sureliya Jan 15 '20 at 10:25
  • It (probably) won't bring your server down. The Linux kernel tries to gently kill off stuff if the system is running out of memory. Unfortunately this can be things like your mariadb service etc, the choice of process that is killed is unpredictable. Totally agree with @chintansureliya though, you should definitely set a limit well within the bounds of the servers physical RAM. In addition you should try and make your code run lighter. – John Hunt Oct 31 '22 at 10:01
3

You should have 64-bit OS on hardware that supports 64-bit OS, 64-bit Apache version and the same for PHP. But this does not guarantee that functions that are work with PDF can use such big sizes of memory. You'd better not load the whole file into memory, split it into chunks or use file functions to seek on it without loading to RAM.

Timur
  • 6,668
  • 1
  • 28
  • 37
  • It is 64-bit already: Linux myHOST 2.6.32-5-amd64 #1 SMP Mon Oct 3 03:59:20 UTC 2011 x86_64 GNU/Linux – mikikg Aug 09 '12 at 14:14
  • Apache is stock version 2.2.16-6+squeeze7 from Debian. I think it is 64-bit compiled. – mikikg Aug 09 '12 at 15:26
2

For others who are experiencing with the same problem, here is the description of the bug in php + patch https://bugs.php.net/bug.php?id=44522

Wiggler Jtag
  • 669
  • 8
  • 23
2

You can also try this:

ini_set("max_execution_time", "-1");
ini_set("memory_limit", "-1");
ignore_user_abort(true);
set_time_limit(0);
kishan Radadiya
  • 788
  • 6
  • 14
1

Input the following to your Apache configuration:

php_value memory_limit 2048M
Florent
  • 12,310
  • 10
  • 49
  • 58
0

I had the same problem with commandline php even when ini_set("memory_limit", "-1"); was set, so the limit is in php not from apache.

You should check if you are using the 64bit version of php.

Look at this question about Checking if code is running on 64-bit PHP to find out what php you are using.

I think your php is compiled in 32 bit.

Radon8472
  • 4,285
  • 1
  • 33
  • 41