Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upper memory limit for PHP/Apache

I'm getting the error when I run my PHP script....

Fatal error: Out of memory (allocated 1827405824) (tried to allocate 88800 bytes)

I've added this line to my PHP script..

ini_set("memory_limit","3000M");

This statement does seem to correctly control the memory usage, but I dont seem to be able to get it above about 1.8GB. Its as if the upper memory limit is being restricted somewhere else. I've also added to the php.ini...

memory_limit = 3000M

Does anyone know if the memory is restricted elsewhere?

I'm running a local server with Xampp. I have Windows 7, 64-bit with 4GB RAM. My script uses PHP's GD image library and I get the error when trying to allocate an image reference with ImageCreateTrueColor().

(I know this is a huge amount of memory - but this is just a one-of script, and its just a lot easier to do it this way.)

Thanks.

Update....

@elusive @Orbling I expect everybody's bored whith this question, but here is the simplified code which illustrates the problem.

<?php
    ini_set("memory_limit","4000000000");
    echo "ini_get = " . ini_get('memory_limit') . "<br>\n";
    echo "memory_get_usage = " . memory_get_usage(true) . "<br>\n";
    $bigImageHandle = imagecreatetruecolor(22200, 24800);  //this is line 5
?>

Browser output...

ini_get = 4000000000
memory_get_usage = 524288

Fatal error: Out of memory (allocated 1843396608) (tried to allocate 88800 bytes) in
E:\User\My_Webs\experiments\houseshunting\temp\osMaps\t1.php on line 5

I tested this out with a smaller set of tiles and the memory used by imagecreatetruecolor() and I estimate I need 2.7GB

like image 599
spiderplant0 Avatar asked Dec 09 '10 14:12

spiderplant0


People also ask

What is the maximum PHP memory limit?

Increasing the PHP memory limit The default memory limit is 256M and this is usually more than sufficient for most needs. If you need to raise this limit, you must create a phprc file.

What is the maximum amount of memory that a PHP script executed by Apache?

By default, a PHP script can allocate up to 128 megabytes of memory.

Where can I see PHP memory limit?

You can check it from your php. ini file. Execute php -i | grep "php. ini" on command line and check memory_limit in your Loaded Configuration File.

How much RAM do I need for Apache Web server?

Most operating systems' default Apache configurations are not well suited for smaller servers-- 25 child processes or more is common. If each of your Apache child processes uses 120MB of RAM, then your VPS would need 3GB just for Apache.


4 Answers

Using Acquia Dev Desktop, I had many memory limit crashes.

After having increased the memory limit into PHP.ini.

php_value memory_limit                  1024M
php_value max_execution_time            3000

This issue was less frequent but still occuring ( Especially with Feature Recreate )

Into my httpd.conf I increased the StackThread to 16M

ThreadStackSize 16*1024*1024

And it solved the memory crash issue. Hope it can help

like image 73
thenrion Avatar answered Oct 08 '22 18:10

thenrion


You're running on a 64-bit operating system, but Apache and PHP are likely still 32-bit. If you're using mod_php, apache would be the limiting factor here.

32-bit processes are limited about 2GiB of RAM unless you used the /3GB switch and the software is aware of 3GB support.

That still leaves up about 200 MiB that seems unused, but its small enough that it can be used by various libraries that all have to be loaded in memory

As far as I know, the library usage won't show up in the committed memory, but still counts towards the 2GiB limit (much like device memory counts towards the 4GiB limit on 32-bit windows. Where installing 2 GiB graphics card brings you down to under 2GiB of usable RAM).

Most likely solution? Install a 64-bit PHP, and then dispatch it to that (using a system() call, perhaps)

like image 39
Reece45 Avatar answered Oct 08 '22 18:10

Reece45


In Ubuntu 18.04
Check version PHP: php -v
In my case I have PHP 7.4
Edit file: nano /etc/php/7.4/apache2/php.ini
Search and change memory_limit = 2048M
Edit file: nano /etc/php/7.4/cli/php.ini
Search and change memory_limit = 2048M
Finally: systemctl restart apache2

like image 2
fcva Avatar answered Oct 08 '22 16:10

fcva


Which PHP version are you using?

The memory_limit variable is, or was, contained in a 32-bit integer, so can not go above 2GB.

See: http://bugs.php.net/bug.php?id=39132&edit=1

From the bottom comment on that bug report, it might be the routine that translates the human readable form to a number, try putting it in digits.

like image 1
Orbling Avatar answered Oct 08 '22 16:10

Orbling