Destroy method doesn't free resources

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
adolfo
Posts: 5
Joined: 2011-09-30T02:21:28-07:00
Authentication code: 8675308

Destroy method doesn't free resources

Post by adolfo »

Hi
I have a problem when using imagick for resizing images.
The imagick wrapper works correctly, but I detected that the server's RAM increases to the maximum available.
It looks like the method 'destroy', responsible for releasing the resources associated with object 'imagick' not release these resources.

My process resize a lot of images like a for loop, and in each iteration, the memory decrease slowly and steadily.
The only way to free the memory used is to make a restart apache.

The code i use is:

$image = "http://foto-cache.elcomercio.es/jpg/0/8 ... 932680.jpg";
$imagick = new Imagick();
$imagick->readImage($image);
$imagick->scaleImage(300, 300);
$imagick->clear();
$imagick->destroy();

you can try it by doing a loop 'for ($i = 1; $i <= 1000; $i++)' of this code and looking the decrease of server's RAM with a 'top -d 1'

thanks a lot
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Destroy method doesn't free resources

Post by magick »

Use unset($imagick) instead of $imagick->destroy(); The destroy() method destroys the image but not the object.
adolfo
Posts: 5
Joined: 2011-09-30T02:21:28-07:00
Authentication code: 8675308

Re: Destroy method doesn't free resources

Post by adolfo »

Hi
Using the method destroy memory was not released, so I use also the method unset, but it is the same, RAM memory continues decrease in each iteration.

To simulate concurrent requests (3 request per second during one minute), i use the 'apache jmeter' program.

I tried with this code in a test.php file.

$image = "http://foto-cache.elcomercio.es/jpg/0/8 ... 932680.jpg";
$imagick = new Imagick();
$imagick->readImage($image);
$imagick->scaleImage(300, 300);
header("Content-Type: image/jpg");
echo($imagick);
$imagick->clear();
$imagick->destroy();
unset($image);
unset($imagick);
exit();
adolfo
Posts: 5
Joined: 2011-09-30T02:21:28-07:00
Authentication code: 8675308

Re: Destroy method doesn't free resources

Post by adolfo »

I think that readimage method have a large memory consumption, and this memory isn't free anytime.
Is this true???.
How can i free this used memory????
there is any alternative to resize a lot of images in memory without dicrease RAM memory????

thanks
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Destroy method doesn't free resources

Post by magick »

The OS may not release some memory after its consumed and released. You may need to run a PHP garbage collection cycle. You can also set the memory limit to some low value. See http://www.imagemagick.org/script/resou ... nvironment MAGICK_MEMORY_LIMIT. You can set this as an environment variable or there is probably an Imagick method to set the memory limit. With a low memory limit, pixels are cached to disk which reduces memory consumption but slows down processing (disk I/O is slower than memory I/O).
adolfo
Posts: 5
Joined: 2011-09-30T02:21:28-07:00
Authentication code: 8675308

Re: Destroy method doesn't free resources

Post by adolfo »

hi again.
I tried to run a PHP garbage collection cycle, but, when i call the php method gc_collect_cycles(), it returns number of collected cycles equals to zero.

also, after i call to the garbage collector does not seem to free up memory.

I fixed the MAGICK_MEMORY_LIMIT environment variable to 100M, because i read that a image size of 5000x5000 needs a 80M to process it, and i have some images of this size.
If i reduce the MAGICK_MEMORY_LIMIT the memory consumption is more slow but finally if reaches the maximum of the memory.
Post Reply