|
-
Jul 15th, 2006, 09:50 PM
#1
Re: [2005] Office Garbage Disposal
Setting a variable to Nothing means that that variable no longer refers to that object. If no other variables refer to that object either then that object is flagged as eligible for garbage collection. Calling Dispose on an object explicitly tells that object to release any unmanaged resources it is holding and to also call the Dispose method of any managed objects it is holding so that they may do the same.
You create an object and assign it to a variable. If you then set that variable to Nothing it simply means that that variable no longer refers to that object, but the object is still there. What happens if you do that is that when the garbage collector comes along is it sees that the object has not been destroyed so it will take at least one pass to destroy it, then it will not reclaim the memory it occupies until a later pass. If you Dispose it yourself then the memory will be reclaimed the first time the garbage collector comes around.
The rules of memory management in .NET are very simple. If an object has a Dispsoe method the ALWAYS call it when you're finished with the object. After that, you should set a reference-type variable to Nothing if you're finished with it and it will not or may not lose scope for some time. Setting value-type variables to Nothing is pointless. Likewise setting reference-type variables that soon lose scope to Nothing is also pointless.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|