Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to always destroy() Imagick objects?

There are two methods in Imagick: clear() and destroy().

But I'm not sure when to use them.

For example, do I have to destroy() even after overwriting the only reference I have?

$img = new Imagick($path1);
// do something with $img, then load a different image
$img = new Imagick($path2); // should I have destroyed $img before doing this?

Does Imagick release the resources if I overwrite the reference?

If not, this is both annoying and scary.

like image 818
ChocoDeveloper Avatar asked Jan 22 '13 16:01

ChocoDeveloper


2 Answers

I did a quick and dirty test, image1 is 85kb image2 is 457kb

$start = microtime();

for($i=0; $i<10; $i++){
    $img = new Imagick('./image1.jpg');
    $img->setImageResolution(72,72);
    $img->resampleImage(72,72,imagick::FILTER_UNDEFINED,0);
    $img->destroy();

    $img = new Imagick('./image2.jpg');
    $img->setImageResolution(72,72);
    $img->resampleImage(72,72,imagick::FILTER_UNDEFINED,0);
    $img->destroy();
}

$end = microtime();
$len = $end - $start;
echo number_format($len, 2),'<br /> <br />';

function kb($n){
    return ceil($n/1024);
}

echo 'memory usage - ',kb(memory_get_usage()),' / ',kb(memory_get_peak_usage()),' <br />';

And then I commented out the destroy lines, and ran it again. strangely it seemed to use more memory when using destroy() but only 3 or 4 k. the timer didn't show much different, and when I ran a basic apache bench load test

ab -n 20 -c 5 http://ubunty.local/sandbox/stackexchange/imagick.php

There didn't seem to be much in it.

I was expecting destroy to use less memory. even using image2 in a different variable didn't seem to make a difference

$img2 = new Imagick('./image2.jpg');

If there is a reason to use ->destroy() then it must be down to something I forgot to measure, as far as I can see.

like image 158
CodeMonkey Avatar answered Nov 15 '22 03:11

CodeMonkey


Yes.

Note that clear() is preferred over destroy() according to the docs.

// clear temp files
$imagick_image->clear(); // in your case "$img->clear();"

You can also run a cron to delete the temp files for you, otherwise your server could crash. This is not php code, it is command line code.

# linux command
find /tmp/ -name "magick-*" -type f -delete

# cron
45 * * * * find /tmp/ -name "magick-*" -type f -delete
like image 32
kintsukuroi Avatar answered Nov 15 '22 05:11

kintsukuroi