Is 128MB PHP memory too much?

Today I added a new feature to a content management system that I am creating. Depending on where you load the image, PHP will resize the image to match the designated location. It works very well, but when I try to load a larger image, like a 3MB image, I get a fatal error:

Fatal error: Allowed memory size of 134217728 bytes exhausted 
(tried to allocate 42520 bytes) in...

      

I think 128MB of memory is quite a lot considering that I don't run that much ... at least I don't think so. It tried to allocate another 42520 bytes for the resizing process, but failed.

My question is, should I (A) increase the limit or (B) overestimate why I am using so much memory in the first place? Is 128MB a good number or too large / too small?

Thanks Ryan

Decision

I came to the conclusion that 128MB is indeed too much to resize an image, and I was so focused on finding other options ... like exec () options, that I no longer looked at my "rough" data. It turns out, although my large image was only 2.83 MB, it was over 10,000 pixels wide. This is problem.:)

+1


source to share


5 answers


GD stores images as bitmaps in memory, so I don't rule out that with some JPG images (eg high resolution, highly compressed) the bitmap version can be quite large.

Call memory_get_usage () right before you open and start resizing the image and see if the memory is too large.



Also just FYI, when the script says "(tried to allocate 42520 bytes)" it doesn't mean that it only takes 42520 bytes to run successfully. It just means that at that moment it needed 42520 bytes. A little later, he may have tried to allocate more memory. So, simply adding 42520 bytes to the total memory would probably not commit anything.

+6


source


How do you resize the image? I hope you are using a library function like imagecopyresampled()

? If so, you don't need 128M RAM to resize 3M image. This indicates that you are doing something wrong or ineffective.



+2


source


You don't need to allocate that much memory. Reevaluate your code to reduce its consumption and make sure you don't save data unnecessarily.

0


source


A rough estimate of how much memory is being consumed by a TrueColor image:

width x height x 4

      

If your users are aware of the maximum dimensions (in pixels) of an uploaded image, you can define the minimum RAM you should allocate.

BTW, consider variables used in operations like format conversion, copy, etc.

0


source


With 64mb you have to work a lot. If you need more, you need something that will start cleaning up used memory. 128Mb should be the maximum script, and it really should be a very large script.

0


source







All Articles