MarcGraff
asked on
VB.NET: Unload or disrtoy Object?
How do I unload a class object?
Dim Object1 As New General.Objects()
...Code...
Unload(Object1)
- Marc
Dim Object1 As New General.Objects()
...Code...
Unload(Object1)
- Marc
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Object1 = Nothing
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Does .Net dispose of it when the app is closed or when the procedure is complete?
I ask this because I need it to close when the procedure is complete.
- Marc
I ask this because I need it to close when the procedure is complete.
- Marc
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
SRigney:
I'm actualy trying to close my bran new telnet object :)
Should I force garbage collection? If so how would I do this?
- Marc
I'm actualy trying to close my bran new telnet object :)
Should I force garbage collection? If so how would I do this?
- Marc
Object is disposed when Garbage Collector will pick it up.
It depends on what available memory you have on your computer.
Memory management in .net is a little bit more complicated than the garbage
collector deallocating unreachable objects. For example, on a machine with
lots of free memory, the CLR might allocate a lot of memory to aid
performance - even though it is not all used. I'm not sure of the exact
algorithms used, but I know it's sufficiently complicated and well hidden
that I shouldn't worry about memory leaks.
One exception to this is if you are using unmanaged resources (e.g. calls to
COM objects, graphics methods, files, database connections etc.). If you
don't manually release these objects (By calling dispose or another
mechanism), you will end up with memory leaks.
In a nutshell, in .NET you don't/won't know for sure when an object is
removed from the memory heap. You will know when it falls out of scope and
the Dispose method will fire at that point. Finalize will fire when the
object is actually being removed from the heap, but since you don't know for
sure when that is, you want to be very careful about using it.
The statement
GC.Collect()
does not stat GC immediately just mark that object is ready for collection
It depends on what available memory you have on your computer.
Memory management in .net is a little bit more complicated than the garbage
collector deallocating unreachable objects. For example, on a machine with
lots of free memory, the CLR might allocate a lot of memory to aid
performance - even though it is not all used. I'm not sure of the exact
algorithms used, but I know it's sufficiently complicated and well hidden
that I shouldn't worry about memory leaks.
One exception to this is if you are using unmanaged resources (e.g. calls to
COM objects, graphics methods, files, database connections etc.). If you
don't manually release these objects (By calling dispose or another
mechanism), you will end up with memory leaks.
In a nutshell, in .NET you don't/won't know for sure when an object is
removed from the memory heap. You will know when it falls out of scope and
the Dispose method will fire at that point. Finalize will fire when the
object is actually being removed from the heap, but since you don't know for
sure when that is, you want to be very careful about using it.
The statement
GC.Collect()
does not stat GC immediately just mark that object is ready for collection
One more thing
you can issue the following statement after GC.Collect
GC.WaitForPendingFinalizer s()
WaitForPendingFinalizers method stops the current thread until object is correctly fianlized.
However calling GC.Collect manually is a BAD idea - you must never do it in real application.
You should invoke GC.Coleect method only when app is idle. GC slows down the app noticably
you can issue the following statement after GC.Collect
GC.WaitForPendingFinalizer
WaitForPendingFinalizers method stops the current thread until object is correctly fianlized.
However calling GC.Collect manually is a BAD idea - you must never do it in real application.
You should invoke GC.Coleect method only when app is idle. GC slows down the app noticably
well garbadge collection work by tracing or reference counting.
If an object has no references its cleaned. The other one looks for pointers in the runtime environment they are the "live ones", the rest gets cleaned.
So if u set an object to NULL or nothing. Its flagged for removal.
When an application is closed its removed from the memory. Local variables disappear because of the stack principal. But for the heap u must use "nothing" to indicate u are not needing that memory space.
If an object has no references its cleaned. The other one looks for pointers in the runtime environment they are the "live ones", the rest gets cleaned.
So if u set an object to NULL or nothing. Its flagged for removal.
When an application is closed its removed from the memory. Local variables disappear because of the stack principal. But for the heap u must use "nothing" to indicate u are not needing that memory space.
I think in your case, you just need to make a public close method for you telnet class and in it call m_comm.close
This will release your socket connection.
I just set my object to null in my c# app, which translates to you setting it to nothing.
I tested with allowing it to go out of scope and it doesn't seem to have any problems.
GCCollect could work for you, but you will need to either be in a function that makes the object out of scope or you will have had to specifically set it to null.
If you are in a function like
public sub dosomething
dim objTelnet as New Telnet
' do some code
' now I'm done with my telnet object.
GC.Collect()
' This will not destroy the objTelnet object because it is still in scope and has a reference.
End Sub
If you use
Public Sub DoSomething
' Call the telnet function
calltelnet
' now I'm done with my telnet object.
GC.Collect()
' This will destroy the objTelnet object because it is not in scope and no longer has a reference.
End Sub
public sub calltelnet()
dim objTelnet as New Telnet
' do some code
End sub
This will release your socket connection.
I just set my object to null in my c# app, which translates to you setting it to nothing.
I tested with allowing it to go out of scope and it doesn't seem to have any problems.
GCCollect could work for you, but you will need to either be in a function that makes the object out of scope or you will have had to specifically set it to null.
If you are in a function like
public sub dosomething
dim objTelnet as New Telnet
' do some code
' now I'm done with my telnet object.
GC.Collect()
' This will not destroy the objTelnet object because it is still in scope and has a reference.
End Sub
If you use
Public Sub DoSomething
' Call the telnet function
calltelnet
' now I'm done with my telnet object.
GC.Collect()
' This will destroy the objTelnet object because it is not in scope and no longer has a reference.
End Sub
public sub calltelnet()
dim objTelnet as New Telnet
' do some code
End sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok I think I understand the garbadge collection.
I have a telnet class object. How would I close this type of object out?
- Marc
I have a telnet class object. How would I close this type of object out?
- Marc
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Oh thanks! That did the trick!
- Marc
- Marc
ASKER
I wish I had more points to give on this one :(
- Marc
- Marc