Upper memory limit for PHP/Apache - php

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

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

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)

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

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.

Check your Apache config (e.g., httpd.conf). There's likely an RLimitMEM directive limiting the memory allow for children processes handling requests.
So, you can set your PHP limit all you want, if Apache spawns the process with a memory limit, you can't exceed that.
If you're on a hosted service and have a shared server, likely you don't have access to this config and need to work with your provider. As you can see, it's configuration that applies server-wide... you're not likely going to get them to change this. Then again, if you're looking to spawn bigger than 1.5Gig processes, you prolly should be either solving the problem a different way (others have suggested this) or getting a dedicated server of some kind (e.g. EC2).
For example:
/usr/local/apache/conf
#RLimitMEM 85643200 104857600 # Limit to: 80Mb / process, 100Mb total
RLimitMEM 134217728 537395200 # Limit to: 128Mb / Process, 512Mb total

The issue is likely to be caused by running 32-bit apache and php. Try upgrading these to 64-bit binaries and see if that fixes the issue.

I've had exactly the same problem and after searching a lot I discovered it had nothing to do wih memory limit but with a bug in my code: I had a function using an array, located in external file. In this function I had set the array as "global" but missed to include the file with tis array....
Like the spiderplant0's error, this was giving me an error with a very very huge memory allocation.
So check your code and try to see which part create this error.

Try this
set_time_limit(300);
ini_set('memory_limit', '20000M');

Try this:
#php_value memory_limit 300M
#php_value upload_max_filesize 200M
#php_value post_max_size 200M
#php_value max_execution_time 80000
#php_value max_input_time 80000

try ini_set('memory_limit', '-1');

Related

PHP Fatal error: Allowed memory size in /var/www/html/vendor/composer/ClassLoader.php

Laravel 4.2, Amazon ec2 Linux
PHP Fatal error:
Allowed memory size of 262144 bytes exhausted (tried to allocate 3072 bytes) in /var/www/html/vendor/composer/ClassLoader.php on line 78
TL;DR You need to increase your memory_limit setting in php.ini. You're barely letting your scripts use any memory at all.
The Problem
The error message Allowed memory size of 262144... means you have a memory_limit setting of 256 kb. That is far too low. For most useful applications, you need at least a few MB. I would start with 8MB and see how it goes.
The Solution
Change the memory_limit setting. In theory, you can do this two ways: (1) edit php.ini or (2) use ini_set().
In practice, you cannot always use ini_set('memory_limit', value);. For one thing, some extensions, like suhosin, prevent scripts from setting the memory_limit in this way. For another, you have to be careful how you do it. For example, you proposed using ini_set('memory_limit', '1G');. But the shorthand (K, M, G) only works in php.ini, not in ini_set. You would have to enter an actual number of bytes (e.g., 1073741824). Also, 1G is pretty excessive for most purposes; very, very few non-malicious PHP scripts need anything like that. Even pretty heavy frameworks like WordPress typically run well within 64 MB or so, even with lots of plugins loaded up.
How to change the setting in php.ini
Figure out which php.ini file you are using (it's not uncommon to have several floating around, depending on how you installed things). You can do this in two ways:
Put this in a .php file and run it: <?php phpinfo();
On a command line, type php -i | grep php.ini. You should see a line that says something like Loaded Configuration File => /etc/php.ini (the output may vary by system, of course).
Edit the file you just found by changing the line that starts with memory_limit = to something more appropriate, like
memory_limit = 8M
Feel free to bump that number up as needed, but I would recommend starting small (not 1G) to prevent bringing your server to its knees accidentally.
One caveat: if this is a testing/development machine and you are running a debugger or profiler like xdebug, you may want to start much higher, and 1G is not insane. But don't start with such a high number on a production machine; work your way up to it.

PHP Fatal error out of memory over 1 GB (should be unlimited) [duplicate]

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
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
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)
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
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.
Check your Apache config (e.g., httpd.conf). There's likely an RLimitMEM directive limiting the memory allow for children processes handling requests.
So, you can set your PHP limit all you want, if Apache spawns the process with a memory limit, you can't exceed that.
If you're on a hosted service and have a shared server, likely you don't have access to this config and need to work with your provider. As you can see, it's configuration that applies server-wide... you're not likely going to get them to change this. Then again, if you're looking to spawn bigger than 1.5Gig processes, you prolly should be either solving the problem a different way (others have suggested this) or getting a dedicated server of some kind (e.g. EC2).
For example:
/usr/local/apache/conf
#RLimitMEM 85643200 104857600 # Limit to: 80Mb / process, 100Mb total
RLimitMEM 134217728 537395200 # Limit to: 128Mb / Process, 512Mb total
The issue is likely to be caused by running 32-bit apache and php. Try upgrading these to 64-bit binaries and see if that fixes the issue.
I've had exactly the same problem and after searching a lot I discovered it had nothing to do wih memory limit but with a bug in my code: I had a function using an array, located in external file. In this function I had set the array as "global" but missed to include the file with tis array....
Like the spiderplant0's error, this was giving me an error with a very very huge memory allocation.
So check your code and try to see which part create this error.
Try this
set_time_limit(300);
ini_set('memory_limit', '20000M');
Try this:
#php_value memory_limit 300M
#php_value upload_max_filesize 200M
#php_value post_max_size 200M
#php_value max_execution_time 80000
#php_value max_input_time 80000
try ini_set('memory_limit', '-1');

memory_limit not work

Fatal error: Allowed memory size of 104857600 bytes exhausted (tried
to allocate 32345609 bytes)
It mean I need 137203209 bytes memory limit to run my php script.
I got this error although I have the following code in the script.
ini_set('memory_limit', 268435456); # 256 MB
The script is on my shared hosting server. I don't have access to php.ini.
The memory_limit did work for other scripts in the same hosting server.
When I checked phpinfo() of my server, I see these
suhosin.memory_limit - 128
memory_limit - 100M
My script definitely needs more than 128M because it is a mail sending script with large file attachment.
You're on a shared host, and shared hosts almost never let you using the ini_set('memory_limit',XXX) functions (otherwise everyone would always try to grab the whole server memory and lock up the whole server). Check phpinfo() to see if safe-mode is on... it probably is.
Also, side-note: if you want to set the memory limit to 256MB in any case, you can just use the:
ini_set('memory_limit', '256M');
notation instead of writing out the whole integer.
suhosin.memory_limit - 128 does not allow you to increase memory limit higher that 128mb, set it to 268435456 to be allowed to increase memory_limit up to 256Mb. I doubt you can do it on a shared hosting, though, because you need an access to suhosin ini file (you can not do something like ini_set('suhosin.memory_limit', 268435456);).
More about suhosin.memory_limit

php serialize memory_limit issue

my script, serializing a large array was working without problems on PHP 5.3.8 with APC. My server crashed I installed PHP 5.3.10 with APC and I get following error.
Allowed memory size of 31457280 bytes exhausted (tried to allocate 262263 bytes).
I increased memory_limit to 256M in php.ini. On same script I verified with PhpInfo() and it is showing 256 MB. However I get the same error message. I disabled APC, and same error message again.
Well, its pretty clear that 31457280 bytes is 30 MB, therefore the limit has not been increased, so i'd check that again.
To make this answer more useful, you should probably be looking at serialising this large array in batches, as its never a good idea to hog so much memory at one time.
Also, you should probably look into igbinary since the native way PHP stores and serialises array is very poor and memory inneficient
Call phpinfo() to check if the memory_limit is actually changed. Maybe you simply edited the wrong php.ini file.
change in php.ini
; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 300M
change memory_limit according to your needs.
or for detail, you goto http://php.net/memory-limit

Symfony Max Allowable Size Error

I just scaffolded a new module and when I save using the generated form I get:
Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to allocate 19456 bytes) in /Applications/MAMP/htdocs/ats/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Hydrator/Graph.php on line 404
Any ideas why?
symfony is quite greedy for memory, so this sometimes happens. You can increase the memory available to PHP/symfony via php.ini as suggested above:
php.ini:
memory_limit = 128M
128M here is just an example, but one which you might need to evaluate.
because that's the configured memory limit of your php. you may want to find what php.ini it uses from phpinfo() and edit it to give symfony more memory.
The memory_limit is the maximum amount of memory a PHP script is allowed to use. Basically, this is a security configuration option, to ensure you don't have a PHP script that does mad and consumes all the memory of the server -- or worse, that you don't have several PHP script that eat more memory than the server has.
This configuration directive can be set in the php.ini file ; it's the file that sets the configuration of PHP.
To find out where the php.ini file is on your server, you can use the phpinfo() function : somewhere near the top of the output, there should be a "Loaded Configuration File" option.
Which value should be used for memory_limit is an interesting question... In the past, when we were only writting small script, 8MB was generally enough.
Now, with Frameworks, bigger applications structured in layers, ORM, and all that, 8MB is generally not enough (as you obviously noticed) -- I generally set memory_limit to 32M on my production servers, which is almost always enough for my applications, without being too much.
So, in my php.ini file, I have :
memory_limit = 32M
Note : it would be tempting to put a very high value for memory_limit, to just get rid of the problem... But remember that memory_limit is here as a security : you should make sure your server has enough memory to answer several requests at the same time !

Categories