Clearing memory/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
Jack-S

Clearing memory/resources?

Post by Jack-S »

Hi,
It seems there are three functions related to clearing the memory IMagick uses: removeImage, clear and destroy. Could you explain the difference between these, and which one should I be using when? Also, given that PHP is supposed to clean up any objects that are no longer used, is it actually necessary to destroy/clear the IMagick objects at all?

Thanks,
Jack
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: Clearing memory/resources?

Post by mkoppanen »

Jack-S wrote:Hi,
It seems there are three functions related to clearing the memory IMagick uses: removeImage, clear and destroy. Could you explain the difference between these, and which one should I be using when? Also, given that PHP is supposed to clean up any objects that are no longer used, is it actually necessary to destroy/clear the IMagick objects at all?

Thanks,
Jack

removeImage
Removes the current image sequence.

clear
Clears the object to the initial stage (removes all images from the object)

destroy
The object is destroyed, all resources are freed.


Consider the following code:

Code: Select all

$images = array( "img1.png", "img2.png", "img3.png" );

$im = new Imagick();

foreach ( $images as $image )
{
  $im->readImage( $image );
  /* Do something with the image */
  $im->writeImage( $image );
  $im->clear();
}
this code would keep only one image at the time in the active memory. Calling readimage without clear would append the image as a new sequence.
Mikko Koppanen
My blog: http://valokuva.org
Jack-S

Re: Clearing memory/resources?

Post by Jack-S »

Ok, cool, thanks.
ridera

Re: Clearing memory/resources?

Post by ridera »

You didn't say for certain; but, I assume when the scrip ends, it clears all the objects from memory
Jack-S

Re: Clearing memory/resources?

Post by Jack-S »

ridera wrote:You didn't say for certain; but, I assume when the scrip ends, it clears all the objects from memory
Yeah, I was wondering about that as well. I would assume the same, but then, I'd rather not take the risk on a production server. If it didn't, and your dealing with large images it could all go horribly wrong very quickly.
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: Clearing memory/resources?

Post by mkoppanen »

ridera wrote:You didn't say for certain; but, I assume when the scrip ends, it clears all the objects from memory

Sorry, forgot to reply to this part. All objects are freed when the script execution ends.
Mikko Koppanen
My blog: http://valokuva.org
Post Reply