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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With