p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

proasp_codeclinic thread: Close variables?

Message #1 by Patrick Mair <pmair@e...> on Fri, 8 Nov 2002 17:01:50 -0500
Excellent.  That's exactly the kind of background I was looking for!


-----Original Message-----
From: Draco Associates Ltd. [mailto:jmail@d...] 
Sent: November 9, 2002 10:27 AM
To: Code Clinic
Subject: [proasp_codeclinic] RE: Close variables?

Patrick, it sounds like you may understand this already, but just in
case you don't... Your variables all have a certain 'scope' depending on
where they are declared.  A variable declared within a function or
procedure only lives inside that procedure and dies and is discarded
when the procedure is completed, for example.  It's good practice to
keep the scope of variables as small as possible, i.e. don't declare a
variable at the top of a page if you only need it inside one of the
page's procedures or functions.

Since your variables all die when execution leaves their scope, you
theoretically should not have to do anything to kill them.  However
explicitly killing all variables which are *OBJECTS* (which includes
most variables related to your use of ADO recordsets) by setting objVar
= Nothing before you leave their scope is important because most of them
otherwise don't disappear from memory even though they are no longer
addressable.  The gathering detritus can drag down your server, even
kill it eventually.

The good news is that many system objects - and especially many ADO
system objects - are really good at reusing themselves even AFTER you
have explicitly killed them.  Setting them to Nothing releases their
resources and lets your system run efficiently, but a vestige of them
remains in memory until something else needs the space.  Therefore if
you set a Connection object to Nothing, for example, and then shortly
afterwards create a new Connection object, if the Connection object you
killed hasn't been overwritten it will be efficiently reused to create
the new object.


-----Original Message-----
From: Patrick Mair [mailto:pmair@e...] 
Sent: Friday, November 08, 2002 5:06 PM
To: Code Clinic
Subject: [proasp_codeclinic] RE: Close variables?

Ack, bit of a typo there.  I HAVE declared all my variables.  

-----Original Message-----
From: Patrick Mair [mailto:pmair@e...] 
Sent: November 8, 2002 3:02 PM
To: Code Clinic
Subject: [proasp_codeclinic] Close variables?

Cheers to Ken and Draco for your help with Option Explicit.


Very basic question - I've not declared all my variables, connections,
commands, ect.  Currently I've only been closing recordsets and then
setting them to nothing.  Should I be doing something to close all my
variables? Just certain ones?



  Return to Index