Home > Software design >  Do need use imagedestroy() before unset Array in PHP?
Do need use imagedestroy() before unset Array in PHP?

Time:12-04

So i got array like this:

$array = [];
$array[0] = imagecreatetruecolor(10, 10);
$array[1] = imagecreatetruecolor(10, 10);
...
$array[1000] = imagecreatetruecolor(10, 10);

All elemets in array above is GdImage. So, do i need to do something like this before unset array to free memory?

foreach($array as $value)
{
   imagedestroy($value);
}
$array = null;
unset($array);

or just do?

$array = null;
unset($array);

CodePudding user response:

Reference https://www.php.net/manual/en/function.imagedestroy.php says:

8.0.0 This function is a NOP now.

So, there's no need to call it. Just unset() or set to null as you asked. The garbage collector will free the memory.

  • Related