Unset()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Unset()

Post by kaisellgren »

Hello,

I've been lately thinking about the unset() function. Let's take a quick sample code from my cache class:

Code: Select all

public function cached_sql_fetch_row($result)
 {
  $array = $result[$this -> sqlrowpointer++];
  $new_array = array();
  foreach ($array as $row_name => $row_value)
   $new_array[] = $row_value;
  return $new_array;
 }
At this point, would PHP automatically unset $new_array and $array after this stop using function? Of course they are cleared at the end of the script execution (garbage collector), but during the run - will they be unsetted()? I guess no. So should I do that myself? I think no since the data is minimalistic. The $array is perhaps from 0 to 15 bytes long and the $new_array a bit more (a figure like 1 kB sounds normal). Since allowed memory usage is usually at least 8 MB, that means I am far from the border.

But where do you set the "limit"? What if I'm working on an $array that is 500 kB in size? What about an array of 1 MB in size? Shouldn't I unset those?

Maybe I should check the size of the array and then determine whether unset(), am I being completely overcomplicated (sounds funny :)) here?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Unset()

Post by requinix »

unset() does not free memory, it merely marks a variable as unused. PHP's garbage collector will automatically determine what variables can be freed: unsetting something to gain a few bytes of memory is pointless. It's smart enough to deal with memory usage.

To answer your questions, $array will be recycled back into memory when the function returns. $new_array won't be because of how PHP handles variables: it's smart enough to realize that it doesn't need to make a copy of something when it will destroy the original a couple instructions later. Essentially the $new_array in your function and (whatever variable gets the result of this function) will be references to the same chunk of data.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Unset()

Post by kaisellgren »

As from what I understand the unset() marks the variable so that PHP may unset it when it has available CPU cycles (or when it's running out of memory it will first unset those which you marked to avoid memory exhaustion). Instead if you want to instantly remove something from memory you could replace it with $array = NULL. Is this right?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Unset()

Post by requinix »

Theoretically if you set something to NULL the original data should have been disposed of and replaced with a "this variable is NULL" flag or bit sequence or something to that effect.
As for what actually happens I don't know. That data may float around for a bit until it gets removed, or the variable may simply be marked as unused while the data remains, or it may behave like you'd expect, or even some weird combination of everything.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Unset()

Post by kaisellgren »

tasairis wrote:Theoretically if you set something to NULL the original data should have been disposed of and replaced with a "this variable is NULL" flag or bit sequence or something to that effect.
As for what actually happens I don't know. That data may float around for a bit until it gets removed, or the variable may simply be marked as unused while the data remains, or it may behave like you'd expect, or even some weird combination of everything.
Okay thanks for that.
Post Reply