1

i'm experiencing some fatal errors when using CURL to retrieve remote images.

Error Message: PHP Fatal error: Out of memory (allocated 786432) (tried to allocate 266241 bytes)

I have a function designed to retrieve the remote image and return it to a variable:

function download_file($file, $ref)
{   

    $curl_obj = curl_init();    
    curl_setopt($curl_obj, CURLOPT_URL, $file);
    curl_setopt($curl_obj, CURLOPT_HEADER, 0);
    curl_setopt($curl_obj, CURLOPT_REFERER, $ref);
    curl_setopt($curl_obj, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($curl_obj, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl_obj, CURLOPT_MAXREDIRS, 3);                           
    curl_setopt($curl_obj, CURLOPT_FOLLOWLOCATION, TRUE); //followlocation cannot be used when safe_mode/open_basedir are on
    curl_setopt($curl_obj, CURLOPT_SSL_VERIFYPEER, FALSE);               
    if(!$downloaded_binary = curl_exec($curl_obj)){
            $downloaded_binary = false; // downloading failed, return false
    }    

    curl_close($curl_obj);
    unset($curl_obj);
    return $downloaded_binary;
}

The failure occurs when curl_exec is called.

Background

  • The failure does not occur on a specific request
  • The memory limit has been increased to 3GB
  • Imagick is used to perform image conversion tasks
  • Each image is under 100kb
  • Some documents contain 500-600 individual pages
  • Running PHP 5.4.9 (windows 64 bit)
  • This is run through the command line and is not attached to a web server
  • Average processing time per record (includes all images in record) is 3.5 seconds.

The application itself exports image (TIFF) data from a proprietary document management system, converts the associated images into a single PDF document and writes the files out to a logical structure.

The failure does not make sense to me, since each curl instance should be a new handle.

Any help or direction on this would be greatly appreciated!

EDIT

I believe I found the resolution to the issue, by adding the following lines: IMagick::setResourceLimit(imagick::RESOURCETYPE_MEMORY, 3072); IMagick::setResourceLimit(imagick::RESOURCETYPE_MAP, 3072);

Found in this post: PHP Imagick memory leak

Community
  • 1
  • 1
Nicholas B
  • 11
  • 3
  • You need to unset the return value too if you don't need it anymore. Maybe this is the cause for leaking memory. – rekire Dec 19 '12 at 20:01
  • Thanks for the reply! Where would you suggest unlinking it? Does not the return statement end the function execution? Or am i mistaken? it would seem logical to unset($downloaded_binary) after the return statement, but i am uncertain this will get executed. – Nicholas B Dec 19 '12 at 20:23
  • I have made two modifications. 1. Added $downloaded_binary=null; to the beginning of the function 2. Added gc_collect_cycles(); to the end of the main loop. This loop was not in a function. The application is running and i'm hopeful that this will resolve the issue. – Nicholas B Dec 19 '12 at 22:01

0 Answers0