Tom, (or anyone,)
Related to my other posts in this thread and Dispose in general, maybe you
can help me with the following.
I've noticed that in my code I do the following:
somearray(sub) = Nothing ' get rid of any existing thingamajig
Completely unnecessary, and likely optimized out of a release compile anyway.
The fact is that the line below, will automatically orphan the reference and
make the old reference ready for garbage collection...
somearray(sub) = New thingamajig
somearray(sub).stringvalues = Split(inputbuffer, ...
But I know from some debugging code I have in my app that if I have a
DataGridView, called dgv, that dgv = Nothing does not really free the
storage. I have to do a dgv.Dispose.
Yep..
So could the code above be causing my memory leak? My thingamajig class is
Are you sure you have a memory leak? What symptoms do you see? Many people
confuse windows memory management and GC as a memory leak when they are not.
First, GC will usually only run when the system in under some sort of memory
pressure - usually the heap has become fragmented so gc will run to clean up
the unused objects and relocate stuff for more efficient access..
And second, even if the gc does run and actually cleans up the memory -
windows may not reclaim it. Memory allocated to a process will often stay
allocated to the process, for performance reasons. It will be reclaimed if it
is unused and the OS needs the memory.
Another mistake is that people often look at the TaskManager to diagnose these
issues, and that is not the correct place to look. You should look in the
system performance counters and monitor the memory usage there.
literally nothing more than two Integers and a String array. (It doesn't
even have an explicit constructor.) My Balena book says that a class only
has to implement a Dispose method if it allocates resources other than
memory. (But then I wonder why DataGridView implements a Dispose method? I
don't see why it would be allocating anything other than memory.)
And if my thingamajig class does need to implement Dispose how do you get
rid of Integers and Strings? I get compiler errors when I try, e.g.,
intvar.Dispose or strvar.Dispose. I can do intvar = Nothing and strvar =
Nothing but my experience with DataGridView is that setting something to
Nothing is not sufficient.
Setting something to nothing is in general, completely unnecessary in .NET (in
fact, it was pretty much the same way in VB6 - but, people were confused there
as well). Basically, shared references, module or class level
variables are about the only place that it actually makes sense.