imagedestroy VS unset

Boring days drives the sanest people do craziest jobs.
Glad I'm not one of them. Still, I benchmarked imagedestroy() and unset() function with a little script:

<?php
    $cool = imagecreatefrompng('imagefile.png');
    echo memory_get_usage(),'<br />';
 
      $timeparts = explode(' ',microtime());
  $starttime = $timeparts[1].substr($timeparts[0],1);
	  $cool = imagecreatefrompng('primespiral2000.png');
    //imagedestroy($cool);
    unset($cool);
      $timeparts = explode(' ',microtime());
  $endtime = $timeparts[1].substr($timeparts[0],1);
  echo bcsub($endtime,$starttime,6),'<br />';
    echo memory_get_usage(),'<br />';
    ?>

This script proves unset() uses less memory and it's the better choice. unset() result less memory is most likely because unset() actually delete the variable, while imagedestroy() only clean up the image structure inside GD.

    $cool = imagecreatefrompng('imagefile.png');
    imagedestroy($cool);
echo isset($cool); //1
 $cool = imagecreatefrompng('imagefile.png');
    unset($cool);
echo isset($cool); //echos nothing...


Comments

Anonymous's picture

But who said memory_get_usage know about the usage of GD?

Hi,
I really liked your blog! keep on the good job :D

But this time, In your testing script you're assuming that the function memory_get_usage() knows also about the memory being used internally by GD resources, where from you know that?
Could be that since you're unsetting the variable, PHP no more knows about its existence and therefore fooled about the memory usage...

Mgccl's picture

True... this assumption is

True... this assumption is bad...

Post new comment

  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <span> <fn>
  • Lines and paragraphs break automatically.
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].
  • Mathematical equations and graphs can be added between [tex] and [/tex], [graph] and [/graph] tags.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
(minus five) + = minus eleven
Solve this math question and enter the solution with digits. E.g. for "two plus four = ?" enter "6".
Honey Pot that kill bots