VB.NET when is it appropriate to set Object = Nothing?

When is it appropriate to set an object to nothing?

What about other things, large arrays? array lists? is this suggested or should i just let GC handle it?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
by setting an object to nothing, you are helping the GC. If you set it, the GC can unallocate the object from memory on its first pass. if you don't, the GC first flag the object as potentially removable and on the second pass, if still marked as potential, removes it. you should always call .Close or .Dispose first.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I think it goes out of scope sometimes and gets set to nothing. As emoreau mentioned it is better to call dispose on items when you don't need them anymore.  If you use Try Catch Finally then be sure to put the dispose in the Finally clause-if appropriate--to make sure that the object is disposed and set to nothing.

The GC is non-deterministic...There's no way to know exactly when it will reclaim memory...except that it does so when it needs to.

Sub Main()
     'call X
     'at this point the local variable g in sub X should be nothing...but the memory that was allocated may
     'or may not have yet been reclaimed by the GC; there is just no longer a reference to it..i.e.
     'we can no longer get to that memory
     'it is set to nothing because it was a local variable and went out of scope
     'a global variable...should you need one...doesn't go out of scope.  So there might be a case where
     'you needed it a while and then wanted to free the memory until you needed it again. Setting it to  
     'nothing allows the GC the chance to free it before you need to allocate something different for that  
End Sub

Sub X()
   dim g as new SOMEOBJECT

End Sub
mcaincAuthor Commented:
thanks guys, this is quite helpful

a main reason i am asking & this may be a completely different ball game is that i am using this dll here http://sourceforge.net/project/showfiles.php?group_id=133783 in my .net project

after several hours VM grows very large & nothing is re-claimed (program starts acting strangely, unstable)...

I profiled this with ANTS several times and saw there were 400-500k live instances of string & 200-250k live instances of int32[] (regex) - all related to this dll

i thought that was strange... so i added a button that would set that object = nothing however, the memory was never given back even after another hour or so of waiting

i'm still trying to figure out how to fix that as i don't really know C# and there doesn't appear to be a dispose() that i can call either

i tried to create my own Dispose() in the referencing class but it doesn't seem to do anything either, am I doing something wrong? is there anything i can do here to salvage this (I don't mind having to re-load the object every hour if necessary)?
Imports AIMLbot
Public Class ProgramNET
    Implements IDisposable
    Protected Overrides Sub Finalize()
    End Sub
    Public Sub Dispose() Implements System.IDisposable.Dispose
        Static done_before As Boolean = False
        If done_before Then Exit Sub
        done_before = True
        myBot = Nothing
        myUser = Nothing
        myReq = Nothing
    End Sub
    Dim myBot As New AIMLbot.Bot
    Dim myUser As New AIMLbot.User("user", myBot)
    Dim myReq As AIMLbot.Request
    Dim myRes As String = ""
    Public Sub New()
        myBot.isAcceptingUserInput = False
        myBot.isAcceptingUserInput = True
    End Sub
    Public Function Request(ByVal sUser As String, ByVal sMessage As String) As String
        Dim myReq = New AIMLbot.Request(sMessage, myUser, myBot)
        myRes = myBot.Chat(myReq).ToString
        myReq = Nothing
        Console.WriteLine("response: " & myRes)
        Return myRes
    End Function
End Class

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.