Memory Deallocation in VB.NET

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

I am a little bit confused about memory clean up in
VB.NET. (Damn I never thought I'd miss News and Deletes
from VC++, but at least I was sure there).

If I have some code like:

Public Function SomeFunction(ByVal count As Integer)
Dim Instance as MyClass

For i = 0 to count
Instance = New MyClass(someconstructordata)
Loop


End Function

When I reach the end of the function, do I have a memory
leak, or does VB.NET clean up all that once the variable
goes out of scoop? What about between loops?

Sean
 
* "Sean said:
I am a little bit confused about memory clean up in
VB.NET. (Damn I never thought I'd miss News and Deletes
from VC++, but at least I was sure there).

If I have some code like:

Public Function SomeFunction(ByVal count As Integer)
Dim Instance as MyClass

For i = 0 to count
Instance = New MyClass(someconstructordata)
Loop


End Function

When I reach the end of the function, do I have a memory
leak, or does VB.NET clean up all that once the variable
goes out of scoop? What about between loops?

The garbage collector will delete the objects (not immediately, but they
will be deleted).
 
if you want to clean up real fast like in C++ use the dispose methods to
"dispose" of allocated memory like the delete command does in c++, but
vb.net has a garbage collector that cleans up after you if you do make messy
code
 
..NET uses a garbage collector to clean up memory on the heap. With each
collection cycle, variables and objects that are no longer reachable by code
are discarded automatically. You can search on MSDN for indepth details on
the garbage collector.

Additionally, variables placed on the stack will automatically be discarded
as the stack unwinds (IOW - local variables will get cleaned up as the
functions end). If the local variables are object references, then the
reference will disappear, making the object (which is always placed on the
heap) unreachable, and therefore a candidate for disposal on the next
garbage collection cycle.

-Rob Teixeira [MVP]
 
use the dispose methods to
"dispose" of allocated memory

Dispose doesn't release the memory used by the object. It is a mechanism by
which you can tell an object that you no longer need it so it can do any
cleanup (e.g. Close open files, close database connections or release
unmanaged resources). After you call dispose, and set your reference to the
object null (or nothing in VB), the object becomes "Unreachable" from your
code - the garbage collector can come along at any time and deallocate the
memory used by the object.
vb.net has a garbage collector

The garbage collector is not only for VB.net - it is used by any managed
language (C# etc.) too.
that cleans up after you if you do make messy
code

There is no excuse for messy code ;) Besides, it cleans up even if you make
good code.

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.

HTH,

Trev.
 
Back
Top