Problem with Fatal error: Allowed memory size of 67108864 bytes exhausted

Discussion in 'PHP' started by LongBeachIsland, Feb 4, 2011.

  1. #1
    Hey guy this is my second post in php. Again I am not to familiar with it just trying to fix a few things on my site. I am getting the following error. Fatal error: Allowed memory size of 67108864 bytes exhausted. the area it points me to is in a file functions.php with the code as follows
    Image fast resize function
    function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) {
      // Plug-and-Play fastimagecopyresampled function replaces much slower imagecopyresampled.
      // Just include this function and change all "imagecopyresampled" references to "fastimagecopyresampled".
      // Typically from 30 to 60 times faster when reducing high resolution images down to thumbnail size using the default quality setting.
      // Author: Tim Eckel - Date: 09/07/07 - Version: 1.1 - Project: FreeRingers.net - Freely distributable - These comments must remain.
      //
      // Optional "quality" parameter (defaults is 3). Fractional values are allowed, for example 1.5. Must be greater than zero.
      // Between 0 and 1 = Fast, but mosaic results, closer to 0 increases the mosaic effect.
      // 1 = Up to 350 times faster. Poor results, looks very similar to imagecopyresized.
      // 2 = Up to 95 times faster.  Images appear a little sharp, some prefer this over a quality of 3.
      // 3 = Up to 60 times faster.  Will give high quality smooth results very close to imagecopyresampled, just faster.
      // 4 = Up to 25 times faster.  Almost identical to imagecopyresampled for most images.
      // 5 = No speedup. Just uses imagecopyresampled, no advantage over imagecopyresampled.
    
      if (empty($src_image) || empty($dst_image) || $quality <= 0) { return false; }
      if ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
        $temp = imagecreatetruecolor ($dst_w * $quality + 1, $dst_h * $quality + 1);
        imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $dst_w * $quality + 1, $dst_h * $quality + 1, $src_w, $src_h);
        imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $dst_w * $quality, $dst_h * $quality);
        imagedestroy ($temp);
      } else imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
      return true;
    }
    PHP:
    What is for is a bulk upload for images to my site. Now there is a box to check for delete images after upload. It only appears to get that error when I have the box checked. The simply solution would be to keep the box unchecked except it makes it tedious to keep track of which files I upload and which ones I did not. I was hoping that there is some kid of error in the code above. I already made sure my php.ini file had the line memory_limit = 128M. I am guessing the problem is something to do with a temporary file that is being created somewhere. I have heard of others getting a similar error when their error log is to large. Anyway if anybody has any brilliant ideas please feel free to share the with me the exact line of code the error points to is
    $temp = imagecreatetruecolor ($dst_w * $quality + 1, $dst_h * $quality + 1);
    
    PHP:
    Thanks in advance for anybody willing to take the time to answer this.
     
    LongBeachIsland, Feb 4, 2011 IP
  2. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #2
    there's maybe some loop or something which is exhausting that memory ... tell me which line error appearS?
     
    G3n3s!s, Feb 4, 2011 IP
  3. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The error is Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 4804 bytes) in /home/content/83/7348783/html/admin/function/functions.php on line 3015. Which is either

    Line 3015 on the orginal file is
    or Line 3015 when I download the file off the server. which is
    I will try to attach the file in here http://digistockmedia.com/problem/functions.php

    don't forget to right click and save as
     
    LongBeachIsland, Feb 5, 2011 IP
  4. eugene_x

    eugene_x Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Unfortunately it's really hard to debug such errors without having full access to site. May be somebody with a sharp eye will notice what's going wrong but generally what you need is:

    Track all the path of code: which functions and where are called, what happens in those functions, etc. The line where fatal error points you to is not necessarily the line with the problem, it's just the point at which server ran out of memory while the real problem may be in a totally different place. Once you have the perfect understanding of script logic, you need to use memory_get_usage(). The function allows you to track how much memory is allocated at the current moment. After you put it in many different places and do lots of debugging, you'll be able to spot a strange place.

    For example there might be a loop where php goes through the number of files, reads each of them into a variable, does something to the content, and saves the file. If you put memory_get_usage() inside this loop, you would expect the number to jump around: e.g. you read 1kb file and value jumps by 1kb, then you read 10kb file and value jumps by 9kb, then you read 5kb file and memory usage goes down 5kb. But instead you might notice that memory usage always rises: 1kb, 11kb, 16kb. This would mean that something is wrong inside this loop: after you deal with a file, you are supposed to clean up everything and this doesn't seem to happen in our example. Then you have a closer look and fix the issue.

    I am afraid I can only give a general advice like that. Hope that helps!
     
    eugene_x, Feb 5, 2011 IP
  5. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #5
    hehe we can't downlaod php file :D
     
    G3n3s!s, Feb 5, 2011 IP
  6. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #6
    "Out of memory" errors are not uncommon when you try to do image processing in a web-based environment. I see your site is hosted with GoDaddy, and if you have one of their shared hosting plans, you cannot override their memory limits. It's likely that you will either have to process your images one at a time, or replace this function with the standard PHP function imagecopyresampled(). You might also consider using ImageMagick, which can be more efficient in memory usage than the GD library that comes with PHP.
     
    rainborick, Feb 5, 2011 IP
  7. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well thanks for the pointer I was hoping for an easy solution. Well maybe I will have to go through all theings I need changed and pay somebody that actually know what they are doing.

    G#n#s!s
    Yea I guess your right. The link works fine, it just shows nothing maybe I should have put it in a .zip file
    http://digistockmedia.com/problem/functions.zip

    Thanks for all the help everybody.
     
    LongBeachIsland, Feb 5, 2011 IP
  8. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks richard for the advice, I believe you are correct with not being able to change the memory limits as I can also not use ffmpeg with godaddy, which is a huge drawback for this type of site. However I do not think it is a memory limit thing for some reason. I have used had no problems with other image thumbnail generators like Simple Image gallery for Joomla's k2. Like I said sometimes I can upload and convert 10 images at a time sometimes it gives me an error when I try to upload a single image with this particular script. Like I said I know very little about .php. so maybe it will be best for me to make a note of all the changes and things I need changed and seek some professional help. I wanted to see if there was an easy fix but it doesn't sound like it. I will try to see if I can take your advice and try to incorporate a different script. Thanks again everybody for taking the time to help.
     
    LongBeachIsland, Feb 5, 2011 IP
  9. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I guess with that being said, is there any known scipt for converting a video from .mov to a mp4 h.264 that will be able to work on a shared hosting account. Appearently I can not use ffmpeg on godaddy is there another way to convert these on a shared hosting account, or am I basically out of luck in that department.
     
    LongBeachIsland, Feb 5, 2011 IP
  10. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #10
    I don't think you'll ever be able to do anything more than the simplest image and video processing on a shared host. The programming isn't the issue - it's just the computing resources available in a shared environment are very limited. You'll likely need a dedicated server, or at least a virtual dedicated server.
     
    rainborick, Feb 5, 2011 IP
  11. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Memory size errors are often due to images being created, but not deleted; in a loop, this will require a lot of memory very quickly.
    Looking at the code you posted above, there is no problem visible to me. Look at your entire code, and make sure that you destroy all images that you create.
     
    ThomasTwen, Feb 6, 2011 IP
  12. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks for all the help I actually found a nice cheaping hosting company that supports ffmpeg. If anybody is interested here is the link ffmpeg hosting
     
    LongBeachIsland, Feb 7, 2011 IP
  13. chainas

    chainas Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    You should set memory_limit value to more than 64mb, like 128mb to avoid this message. But first, You should check Your code consistency.
     
    chainas, Feb 10, 2011 IP
  14. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I tried doing this in my php.ini however I believe this does not actually effect anything unless you are have acess to the root directory. Unfortunately I have a shared hosting account. I may be loking into getting a VPS or a dedicated. I did not realize that you could obtain multiple IP addresses with a VPS or dedicated. I am unsure though of how much time I will have to spend keeping the dedicated updated. As I also found the hosting company above supports both Image Magic and ffmpeg.
     
    LongBeachIsland, Feb 10, 2011 IP
  15. chainas

    chainas Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    In shared host You can not configure php.ini files. Then You should take VPS.
     
    chainas, Feb 11, 2011 IP