Print Page | Close Window

How to dispose of a variable?

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: ASP.NET Discussion
Forum Description: Discussion and chat on ASP.NET related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=21523
Printed Date: 28 March 2026 at 7:53am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: How to dispose of a variable?
Posted By: theSCIENTIST
Subject: How to dispose of a variable?
Date Posted: 09 October 2006 at 1:50am
Say I have this;
 
Dim myVar = Timer()
 
I then use and abuse of it and in the end when I no longer need it I want to free resources by disposing it, how do I do that?
 
Should I use;
 
myVar = Nothing
 
Or;
 
myVar = ""
 
What?


-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com



Replies:
Posted By: dpyers
Date Posted: 09 October 2006 at 3:56am
"" is an empty string. It has a string termination character and a pointer to physical address space for the string.

Nothing/Null is truly non-existant. There's no address space involved and no end-of-string character. IIRC, there's still a pointer but it's set to null.

It's a bit more efficient to set a variable to "" if you're going to reuse it later as you don't need to get a new address. If you're not going to use it later, setting it to Nothing/Null will save 32 or 64 bits of initial address space.

Remember that when a script ends, all memory space except that used by objects is rleased into the pool. The exception would be sripts invoked via server.execute or includes that will use the same memory space as the calling script.

EDIT: Stuff like timers are double-words to begin with so you gain a little more my setting them to nothing/null.


-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: theSCIENTIST
Date Posted: 09 October 2006 at 4:34am
Thx, great explanation. Where's the rating system?

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: Mart
Date Posted: 09 October 2006 at 7:23am
You can't set an object to a null string unless it's a string however, the nature of VB.NET is that it is strongly typed.
The only way to get a .NET class to release its resources is to call the .Dispose() method when you're done with it.


Posted By: scottage
Date Posted: 04 January 2007 at 9:09am
Mart, you can only call Dispose for those objects that support the Dispose method. A very good way of ensuring that objects are cleanly removed is to use a using block (I only code C# so I don't know if VB.net supports this)
using (IDataReader dataReader = Database.ExecuteReader())
{
while (dataReader.Read())
{
...
}
}
You can only put an object in the using block that supports the Dispose() method


-------------
http://www.realwebdevelopers.com" rel="nofollow - Developing real world websites



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net