[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Not enough memory to allocate in PHP

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #10141
    kenneth-.j
    Participant

    Background:

    I have an image resizing script that takes an uploaded image and resizes them to a specified size.( 100x 100 and 250×250 ) However, i have an image that keeps causing the error

    PHP Fatal error:  Allowed memory size of 67108864 bytes exhausted (tried to allocate 21600 bytes) in .....
    

    After i’ve done some looking around, i found that the reason this particular image(and no thte others) was causing the error probably due to it’s dimensions.(5400 * 7200 ) After further looking around on SO, i came across the solution to this problem by increasing the memory size in php.ini to 64MB.

    However, from PHP GD Allowed memory size exhausted the amount of memory needed for image manipulation seems to be derived from (width * height * 8 ) which would give (5400 * 7200 * 8 =311040000 ). My php ini filehowever says

    Maximum amount of memory a script may consume (128MB)
    

    which is far less than the required amount of 311040000 which is in the region of 300+mb? If so, is there any thing i can do to solve this problem?

    I’m currently working on localhost.

    Thanks!

    #10143
    eternal1
    Participant

    If you’re working under apache webserver, you can just write the following in your .htaccess file

    <IfModule mod_php5.c>
    
        php_value memory_limit 512M
    
    </IfModule>
    

    This should do the trick.

    #10142
    alister-bulman
    Participant

    You can set PHP to use as much memory as required (even to system memory exhaustion), but setting the limit to ‘0’ – but doing so within anything running within Apache, as mod_php (or, for that matter as a fastCgi, with fpm) is a very bad idea. The reason is that the Apache process will then balloon to the largest size it took, slowing down the system generally an taking up memory long-term, until it is destroyed – this could be days or more.

     php_value memory_limit -1
    

    The command-line version of PHP doens’t normally have the limit even set, making it very suitable for larger tasks.

    If you are going to be resizing large images (which with camera phones these days, is pretty much all pictures), it would be far better to run the conversion out of Apache, from the command line. You might not even need PHP and GD to be able to do that. One way I’ve done this before is with various job-queues – at the simplest, a database table with the list of files to resize, which a command-line process goes through one at a time, and it can also restart itself after each completed image – to ensure that all the memory used, is cleared.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.