A question about Nothing

  • Thread starter Thread starter Dave F
  • Start date Start date
D

Dave F

Hi

I understand what Nothing does:

Set MyObject = Nothing

But I don't understand the reason for it.
Especially at the end of a routine

Doesn't End Sub do the same thing?

Dave F.
 
In an ideal world, The app should tidy up after itself rendering the:

Set Object = Nothing

as an excersize in futility, however some apps, mentioning no names (Access)
have bugs in them which may be avoided by setting object variables to
nothing at the end of the sub or function.

P
 
Hi Dave

No. "Set" allocates memory for something. Say you have an object X that uses 2Mb ram. Then
in theory it goes like this:

Set A as new X '2 Mb allocated for future use
Set B as new X 'and another 2
'.... do stuff with or without A and B, until
Set A as noting 'release A's 2 Mb for use elsewhere
Set B as nothing 'another 2

"In theory" because it doesn't always release, VBA or whatever program. In those cases you
have a "memory leak" and your Pc starts acting sluggish for no obvious or logical reason.
End Sub will or will not release memory, depending on wether the A variable is local or
not, and also on luck (memory leak II ). Anyway it's a very good coding habit to release
memory manually -we do have style after all ;-)
 
Back
Top