php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29558 Memory leak when calling imageCreateFromJPEG and imagecreatetruecolor in loop
Submitted: 2004-08-07 00:14 UTC Modified: 2004-11-27 01:00 UTC
Votes:8
Avg. Score:4.8 ± 0.4
Reproduced:6 of 6 (100.0%)
Same Version:1 (16.7%)
Same OS:1 (16.7%)
From: zsheran at yahoo dot com Assigned:
Status: No Feedback Package: GD related
PHP Version: 4.3.7 OS: Redhat 9
Private report: No CVE-ID: None
 [2004-08-07 00:14 UTC] zsheran at yahoo dot com
Description:
------------
I call the function below from a loop about 17,000 times to resize JPGs which are about 20k each. Note that $source_file is a URL. I run the script from CLI. By the time the script finishes after ~270 minutes, the PHP process uses 340MB of RAM; it grows slowly and steadily to that size. I unset the variables at the end of the function but it makes no difference. Now 340MB is about the size of those 17,000 pics. For some reason, the fetched images are not freed from memory, probaly only upon finishing the script. But since it runs in a loop, it adds up at becomes eventually a problem.  It works but it would be great if the memory leak could be fixed; I expect the number of images to grow to six digit numbers in the future. 

Thanks for your time.



Reproduce code:
---------------
function resize_picture ($source_file, $target_file) {
    $source_picture = imageCreateFromJPEG($source_file);
    if ($source_picture) {

        $picsize = @getimagesize($source_file); // get source image size
        $source_x  = $picsize[0]; // source image width
        $source_y  = $picsize[1]; // source image height

        #create image $target_picture
        $target_picture = imagecreatetruecolor(320, 240);
        #Resize $source_picture and copy it into $target_picture.
        imagecopyresampled($target_picture,$source_picture,0,0,0,0,320,240,$source_x,$source_y);
        #Create jpeg from $target_picture. Save as $target_file
        imagejpeg ($target_picture,"/path/$target_file",80);
        unset($source_picture);
        unset($target_picture);
    }
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-07 04:43 UTC] smacvicar at gmail dot com
http://uk.php.net/manual/en/function.imagedestroy.php
 [2004-08-08 05:34 UTC] zsheran at yahoo dot com
Thanks smacvicar.

I made the following change at the bottom of the function:

replaced 

unset($source_picture);
unset($target_picture);

with

imagedestroy($source_picture);
imagedestroy($target_picture);

The problem persists. It still leaks memory the same way it did before. 

I didn't know imagedestroy() had to be used instead of unset(); my fault. But now it looks even more like a bug to me...
 [2004-08-10 10:09 UTC] klaus at deam dot org
maybe this is related to bug #29460?
 [2004-11-19 13:46 UTC] pajoye@php.net
Are you talking about real memory leaks (memory not freed at the end of the process) or about the memory used by php?

--Pierre
 [2004-11-27 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 15:01:32 2024 UTC