PHP out of memory, last chance function call? [duplicate] - php

I get this error when users are uploading images on my site.
error msg is "PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 12352 bytes) in /home......."
How can I fix this using php.ini?
Here is my current upload php.ini settings
upload_max_filesize = 2000M ;
post_max_size = 2000M
max_file_uploads = 8
Any ideas what else I need to add to solve this error?

The optimal memory_limit value depends on what you are doing with the uploaded files. Do you read the files into memory using file_get_contents or the GD library? In that case, increase memory_limit to at least the same as upload_max_filesize, preferably more.
If you are using GD, keep in mind that GD holds the entire image uncompressed in memory. This means that it takes memory in the range of width * height * bit-depth, e.g., 1024*768*32 = 25 165 824 bits = 3 MB for a screenshot, or as much as 55 MB for a 14 megapixel image.
Some operations may need to create a copy of the image, so consider setting memory_limit to the double of what you need to keep the image in memory. Also make sure to not load all images into memory at once if you don't have to. You can free the memory used by GD by calling imagedestroy on the handle when you are done working with the image.

set_time_limit(0);
ini_set('memory_limit', '20000M');
To the top of your script. Change the 20000M accordingly.

Increase your memory limit from php.ini
memory_limit = ...

Uploaded files are saved in memory, so you should also to increase memory at least the same size of the expected file.
memory_limit = 2000M // better 2200M or above, just in case.

Related

php image copy/crop/resize memory limits [duplicate]

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 4912 bytes) in /var/www/development/example/system/libraries/Image.php on line 130.
The JPEG image in question does not have a particularly large file size (741 KB). We've used this same code to rebuild larger images. However, the image does have unusually large dimensions (4912px x 3264px). Would this have an effect?
What determines memory usage when PHP is rebuilding an image? Is it just the file size? The dimensions? The colour density? The file type?
The line on which it broke was
$f1 = 'imagecreatefrom' . $tag;
$src = $f1($file);
I think that's enough context. It didn't get as far as trying to rebuild the image. Loading it into memory was enough to break it.
As riky said, set the memory limit higher if you can. Also realize that the dimensions are more important than the file size (as the file size is for a compressed image). When you open an image in GD, every pixel gets 3-4 bytes allocated to it, RGB and possibly A. Thus, your 4912px x 3264px image needs to use 48,098,304 to 64,131,072 bytes of memory, plus there is overhead and any other memory your script is using.
Increase your memory buffer size
php_value memory_limit 64M in your .htacess
or ini_set('memory_limit','64M'); in your php file
It depends your implimentation. last time when I was working on csv file with more then 500000 records, I got the same message. Later I introduce classes and try to close the open objects. it reduces it memeory consumption. if you are opening an image and editing it. it means it is loading in a memory. in that case size really matter. if you are operating multiple images. I will record to one per image and then close that image. In my experience when I was working on pdf artwork files to check the crop marks. I was having the same error.
//you can set the memory limits values
// in htaccess
php_value memory_limit 64M
//or in you using following in php
ini_set('memory_limit', '128M');
//or update it in your php.ini file
but if you optimize your code. and use object oriented aproach then you memory consumption will be very less. because in that every object has its own scope and out of that scope it is destroyed.
The size of the used memory depends on the dimension and the color bit depth.
I also ran in to that problem a few years ago, while building a portfolio-website for photographers. The only way to properly solve this is to switch your image library from GD to imagick. Imagick consumes far less memory, and is not tied to the PHP memory limit.
I have to say that the images the photographers uploaded were up to 30MP. And setting the memory limit to over 1024MB makes no sense in my eyes.

Allowed memory size of 67108864 bytes exhausted (tried to allocate 19200 bytes)

I'm trying to upload a image of 10 mb size and it is giving me the error:
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to
allocate 19200 bytes)
I've also tried to change into .ini file by making changes as:
upload_max_filesize = 70M
post_max_size = 20M
But it still not working... and giving me the same error... its so frustating what can I do next...
What I'm doing wrong with my code, How can I solve this problem?
It is PHP that is running out of memory. You will need to up the memory_limit option in your php.ini, or use ini_set in your PHP (assuming it is enabled).
Please try:
ini_set("memory_limit","100M");
You currently have a memory limit of 64M (that's what the message is trying to tell you), which is far bigger than the 10 megabyte image. Probably you have a problem elsewhere (inefficient script, infinite loop, infinite recursion?).
Don't read your file into memory. You are using over 64MB in your script which is far more than the 10MB from the image, so you're likely reading the file and copying it around and doing other inefficient stuff. (We can but guess without seeing your code.)
Just copy the file directly from its temporary location into its new desired location with filesystem operations (such as move_uploaded_file).
If you definitely have a need for 64MB for some reason other than merely uploading an image — say, perhaps you're performing complex and costly manipulations on it — then you can change the memory_limit INI option. But this should be a last resort as compared to fixing your code. :)
Your PHP ran out of memory. Instead of raising memory limits, you should seriously think if you could enchant your script so that it takes less memory. For example, instead of reading uploaded file to memory, use only file system actions for it.
Correct syntax to use in your php.ini:
memory_limit = 128M
You can also try to change your script to take less memory
You should at least swap the values you have for "post_max_size" (maximum for the entire post) and "upload_max_filesize" (maximum for each actual file)

PHP rebuilding images: memory usage

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 4912 bytes) in /var/www/development/example/system/libraries/Image.php on line 130.
The JPEG image in question does not have a particularly large file size (741 KB). We've used this same code to rebuild larger images. However, the image does have unusually large dimensions (4912px x 3264px). Would this have an effect?
What determines memory usage when PHP is rebuilding an image? Is it just the file size? The dimensions? The colour density? The file type?
The line on which it broke was
$f1 = 'imagecreatefrom' . $tag;
$src = $f1($file);
I think that's enough context. It didn't get as far as trying to rebuild the image. Loading it into memory was enough to break it.
As riky said, set the memory limit higher if you can. Also realize that the dimensions are more important than the file size (as the file size is for a compressed image). When you open an image in GD, every pixel gets 3-4 bytes allocated to it, RGB and possibly A. Thus, your 4912px x 3264px image needs to use 48,098,304 to 64,131,072 bytes of memory, plus there is overhead and any other memory your script is using.
Increase your memory buffer size
php_value memory_limit 64M in your .htacess
or ini_set('memory_limit','64M'); in your php file
It depends your implimentation. last time when I was working on csv file with more then 500000 records, I got the same message. Later I introduce classes and try to close the open objects. it reduces it memeory consumption. if you are opening an image and editing it. it means it is loading in a memory. in that case size really matter. if you are operating multiple images. I will record to one per image and then close that image. In my experience when I was working on pdf artwork files to check the crop marks. I was having the same error.
//you can set the memory limits values
// in htaccess
php_value memory_limit 64M
//or in you using following in php
ini_set('memory_limit', '128M');
//or update it in your php.ini file
but if you optimize your code. and use object oriented aproach then you memory consumption will be very less. because in that every object has its own scope and out of that scope it is destroyed.
The size of the used memory depends on the dimension and the color bit depth.
I also ran in to that problem a few years ago, while building a portfolio-website for photographers. The only way to properly solve this is to switch your image library from GD to imagick. Imagick consumes far less memory, and is not tied to the PHP memory limit.
I have to say that the images the photographers uploaded were up to 30MP. And setting the memory limit to over 1024MB makes no sense in my eyes.

imagecreatefromjpeg + Out of memory problem

Hello All i have following ini variable set in phpini file.
max_execution_time 50000
memory_limit 40M
post_max_size 8M
When i try to make thumbnail of image its gives me following error :
Fatal error: Out of memory (allocated 30670848) (tried to allocate 14976 bytes)
image size is 700 kb.
can any one help me ?
Your problem is not that single call to imagecreatefromjpeg() but memory that you have allocated earlier. After all, the allocation fails with a memory request for only ~14kB.
Maybe you created images before in the same script without releasing their memory with imagedestroy() or you have another memory problem. In the latter case you could use a debugging tool (e.g. webgrind) to find the memory hog.
What is the resolution of your 700kb image? It doesn't really matter how big the original .JPG is. If it's a very "simple" image, it could literally be 10,000 x 10,000 pixels. When it's loaded/uncompressed by PHP, those pixels will require 10k x 10k x 3 = 286 megabytes (10x squared times 3 for each red/green/blue component value).
And of course, you say you've set memory limit to 40M, but you've run out of memory at just 29.25 meg. Could be that there's an override somewhere in the setup, perhaps in a site-specific web server .conf file, or a .htaccess, which is setting a lower limit than 40M.

Prevent memory overflow

At some point in my php script that makes use of curl, the following error shows up: "Fatal error: Allowed memory size of 262144 bytes exhausted (tried to
allocate 77824 bytes) in"... It points out this part of the script: "$s = curl_exec($c);"
What is the problem? And how to settle it down?
In this case, your server is misconfigured.
Allowed memory size of 262144 bytes
200 kilobytes of RAM per script are not enough for most PHP scripts. The standard in my experience is 8 MB minimum; 16 MB is normal. A blog system like WordPress (it is admittedly fat, but still one of the most popular blog systems around) chokes on 8 MB and runs half-way decently with 16.
You should change the memory_limit value in your php.ini. If you're on shared hosting, demand that the provider increase it to at least 8M, better 16M or more. If they deny, get out of there: It's sub-standard hosting.
you are tying to allocate more memory than the heap can handle
set your limit higher, for xample
at the top of the script::
ini_set("memory_limit","10M");
or in your php.ini
memory_limit = 10M
this set your memory_limit to 10M
ini_set() is probably better than setting something in php.ini. If you've got a specific application you know needs more than the standard memory - then it's fine to increase the memory limit for that application. You want to be really careful opening up all your code to having a higher memory limit though.
That said, if you set memory_limit to 0, there is no memory limit & the script will use as much memory as it needs (and the system can give it).
Here are a couple of suggestions:
edit your php.ini file and change the line that says memory_limit = .25M so that it says
memory_limit = 16M
make sure you are calling
curl_close($c);
consistently.
If you want to include a larger code snippet, maybe we can see where you have memory leaks.

Categories