Page 1 of 2
common practice?
Posted: Sun Mar 26, 2006 1:13 am
by s.dot
I know it's common practice to free mysql result sets. But is it common to unset() all variables and arrays before the script finishes?
I've been doing this a lot lately. Is there any case where this could hurt the execution of the script?
Posted: Sun Mar 26, 2006 1:18 am
by feyd
I'm not aware of it having ill effects on the script per se. I have had a few situations where objects weren't being deleted properly (all children being deleted first) but that happened a bit random (switching machines would change behaviour too, truely not good). Switching to 5.1 fixed it so far though.
Posted: Sun Mar 26, 2006 1:28 am
by s.dot
so is it good practice? especcially with scripts that consume a lot of memory
is it common practice?
Posted: Sun Mar 26, 2006 1:34 am
by feyd
it's good practice to dump variables when you no longer need them.
Posted: Sun Mar 26, 2006 2:37 am
by matthijs
Interesting. I haven't done it myself, so far. Probably because in most (all?) tutorials, articles and books I've read it isn't done. But I can see the "good practice" issue about it. Especially when apps get more complicated with multiple includes etc.
Re: common practice?
Posted: Sun Mar 26, 2006 4:37 am
by Ree
scottayy wrote:But is it common to unset() all variables and arrays before the script finishes?
I really don't see the point to do this
right before the end of the script.
Posted: Sun Mar 26, 2006 5:25 am
by s.dot
i didn't mean right before the end of the script
im talking like i get obsessive with it lately =/
like this
Code: Select all
$array = array();
for($i=0;$i<$whatever;$i++){
$array[] = $i
}
unset($i);
unset($whatever);
// do stuff with $array
unset($array);
// do more code
Posted: Sun Mar 26, 2006 7:44 am
by Ree
In this case it makes sense, yes. However, I find myself doing it very rarely, usually in cases when I manipulate huge collections. I really never care about unsetting a month array or anything of this 'caliber'.

Posted: Sun Mar 26, 2006 8:26 am
by Buddha443556
Code: Select all
$array = array();
for($i=0;$i<$whatever;$i++){
$array[] = $i
}
unset($i, $whatever); // one call
// do stuff with $array
unset($array);
// do more code
I really don't see the point to do this right before the end of the script.
I've seen code from Ilia Alshanetsky where he unset() just before an exit which makes me wonder.
Code: Select all
//From: Managing PHP Performance By Ilia Alshanetsky
function cache_start()
{
global $cache_file_name;
// a superbly creative way for creating cache files
$cache_file_name = __FILE__ . '_cache';
$age = 600; // default cache age
// check if cache exists and is valid
if (@filemtime($cache_file_name) + $age > time()) {
// Yey! cache hit, output cached data and exit
readfile($cache_file_name);
unset($cache_file_name); exit;
}
ob_start(); // nothing in cache or cache is too old
}
Posted: Sun Mar 26, 2006 9:34 am
by feyd
It never hurts to help php clear memory. What if the version you are working on has some memory leaks? If memory serves, a while ago if you didn't clear certain things it would leak in GD or some other commonly used extension.
Re: common practice?
Posted: Sun Mar 26, 2006 2:39 pm
by alex.barylski
scottayy wrote:I know it's common practice to free mysql result sets. But is it common to unset() all variables and arrays before the script finishes?
I've been doing this a lot lately. Is there any case where this could hurt the execution of the script?
It's not nessecary at all...
http://ca.php.net/manual/en/language.ty ... f-destruct
GC takes care of that for you
I think the primary reason for PHP even having those functions are for occassions when you need to explicitly free a resource (mysql connection, file handles, etc) so you can create a new one and assign it to the same variable.
Code: Select all
$fp = fopen('somefile.txt', 'r');
$buff = fread($fp, filesize('somefile.txt'));
fclose($fp);
$fp = fopen('somefile2.txt', 'r');
$buff2 = fread($fp, filesize('somefile2.txt'));
fclose($fp);
PHP functions like
mysql_* are typically just wrappers around the original c/c++ API and those languages don't have GC so explicit freeing of resources is mandatory.
Explicit freeing of resources may also be required in PHP if you were using GTK (or similiar) to develop desktop applications, etc...where scripts could run for weeks at at a time without termination.
So, IMHO your being redundant...

Posted: Sun Mar 26, 2006 2:54 pm
by Chris Corbyn
I agree with Hockey here. From what I've read, including in the Zend documentation for writing extensions, the ZE has some advanced memory management features. Then again, what feyd mentioned about memory leaks in <place-name-of-some-big-library-here> rings a bell.
Posted: Sun Mar 26, 2006 3:35 pm
by s.dot
scottayy wrote:Due to the reference-counting system introduced with PHP 4's Zend Engine, it is automatically detected when a resource is no longer referred to (just like Java). When this is the case, all resources that were in use for this resource are made free by the garbage collector.
If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)
Posted: Sun Mar 26, 2006 3:43 pm
by alex.barylski
scottayy wrote:Code: Select all
Due to the reference-counting system introduced with PHP 4's Zend Engine, it is automatically detected when a resource is no longer referred to (just like Java). When this is the case, all resources that were in use for this resource are made free by the garbage collector.
If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)
I'm not sure what my local PHP (version 3.2 something) config is, whether I have STRICT set or whatver it's called, but I just called
unset() twice sequentially and didn't get any error. I called unset() on a variable which wasn't declared or defined yet.
Can you give me the error message? I'm curious what it says...
Posted: Sun Mar 26, 2006 3:44 pm
by Chris Corbyn
scottayy wrote:scottayy wrote:Due to the reference-counting system introduced with PHP 4's Zend Engine, it is automatically detected when a resource is no longer referred to (just like Java). When this is the case, all resources that were in use for this resource are made free by the garbage collector.
If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)
I think that basically mean there will still be a pointer to the data if it could still be accessed.
For example, when you're inside a function every variable you create will be available only in the local scope of the function, therefore, when the function ends, the memory used to hold those variables will be freed automatically. If you have a global variable defined in your script I don't think ZE will delete it from memory until the script ends.
In other languages (C/C++...) if you create a pointer to something in memory and then you leave the scope of that pointer without freeing the memory up with "delete" you've just lost X Blocks of memory that you can never recover. PHP avoids that with ZE.
Hmm... I guess the use of unset() could be valuable in large scripts where global data is only needed for a short section of the script.