common practice?
Moderator: General Moderators
common practice?
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?
I've been doing this a lot lately. Is there any case where this could hurt the execution of the script?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
so is it good practice? especcially with scripts that consume a lot of memory
is it common practice?
is it common practice?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Re: common practice?
I really don't see the point to do this right before the end of the script.scottayy wrote:But is it common to unset() all variables and arrays before the script finishes?
i didn't mean right before the end of the script
im talking like i get obsessive with it lately =/
like this
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 codeSet Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
- Buddha443556
- Forum Regular
- Posts: 873
- Joined: Fri Mar 19, 2004 1:51 pm
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 codeI've seen code from Ilia Alshanetsky where he unset() just before an exit which makes me wonder.I really don't see the point to do this right before the end of the script.
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
}-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: common practice?
It's not nessecary at all...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?
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);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...
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
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.scottayy wrote:If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)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.
Can you give me the error message? I'm curious what it says...
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I think that basically mean there will still be a pointer to the data if it could still be accessed.scottayy wrote:If this is the case, why doesn't unset() throw out an error like it does if you unset($invalid_variable)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.
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.