How to increase memory limit for PHP over 2GB?
Asked Answered
D

8

67

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.

Dedradedric answered 9/8, 2012 at 14:1 Comment(9)
2G apache processes! Wow.Audreyaudri
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.Principalities
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.Dedradedric
@Dedradedric 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.Audreyaudri
@Audreyaudri 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.Dedradedric
@Dedradedric 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.Audreyaudri
@Audreyaudri 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!)Dedradedric
At least, can you advice me some good shell based HTML2PDF converter?Dedradedric
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_intShirtwaist
G
71

Have you tried using the value in MB ?

php_value memory_limit 2048M

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

Gladygladys answered 9/8, 2012 at 14:4 Comment(4)
yeah, it must be in php.ini. For some reason do not work over 2G in apache configuration (work without a problem other lower values).Dedradedric
hi...actually i am using zend so how to increeze memory of zendTijuana
Is this to say there's practically no limit to how much memory PHP can consume, provided you have enough RAM available?Chorea
Please avoid direct changes in php.ini file.Hackberry
A
60

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

Audreyaudri answered 9/8, 2012 at 14:11 Comment(5)
I know, script is not mine, it is some html2pdf converter and input to it is very large HTML page.Dedradedric
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.Frick
Should go without saying, but make sure you pump the tyres right up before you strap the rocket on.Jessamine
sometimes u need a rocketDaune
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.Noonberg
O
29

For unlimited memory limit set -1 in memory_limit variable:

ini_set('memory_limit', '-1');
Oak answered 7/5, 2014 at 9:52 Comment(3)
It's nice to know that unlimited memory is possible, but, I don't think I'll be using this in my code soon. =)Frick
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.Demarco
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.Beerbohm
W
4

You can also try this:

ini_set("max_execution_time", "-1");
ini_set("memory_limit", "-1");
ignore_user_abort(true);
set_time_limit(0);
Weathertight answered 24/9, 2018 at 15:30 Comment(1)
You should explain your answer, it might not be clear for anyone what this config means. In certain cases it might be quite dangerous to have no limits at all!Kreplach
N
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.

Node answered 9/8, 2012 at 14:10 Comment(2)
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/LinuxDedradedric
Apache is stock version 2.2.16-6+squeeze7 from Debian. I think it is 64-bit compiled.Dedradedric
P
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

Parmenides answered 5/3, 2013 at 9:15 Comment(0)
M
1

Input the following to your Apache configuration:

php_value memory_limit 2048M
Munmro answered 9/8, 2012 at 14:4 Comment(0)
S
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.

Shirtwaist answered 4/2, 2020 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.