Question Help me understand leaking mem.

eftwelve

New member
Joined
Jul 31, 2008
Messages
2
Programming Experience
Beginner
Hi, Im new to vb.net but not programming and im having an issue understanding why memory is not being released.
Ive created a class thats similar to binary tree.
pseudo example:
VB.NET:
Public Class tTree
 '.. (other simple data types)
 public A as tTree
 public B as tTree
End class

I can create tree's and they work fine, however As I manipulate a tree, any branch may be replaced by a new branch or null (Nothing). (including the root tree)

In other languages that im used to, a sub branch being replaced by a new one or null will effectively cause the old branch and everything attached to it to go out of scope and collected by the GC.

As I work with a single tree the memory usage just keeps growing and growing, Even if i set the "root tree" (my only static variable in the whole app) to nothing.

As a last ditch effort I tried implementing this proc.
VB.NET:
Public Sub dispose() Implements IDisposable.Dispose
         A = Nothing
         B = Nothing
End Sub

This should effectively GC every tTree instance below the current one.
Again, I only have one global tree variable as a root, calling dispose even on this does nothing to relieve the memory leak.

What am i doing wrong?

Ps: Ive checked that there is not something else accumulating like a static string or anything like that.
Thanks
 
Setting a variable Nothing is just a de-reference, the object is not disposed. .Net GC does not do reference counting. What you should do is dispose A and B if they are referencing objects, ie if they are not Nothing. Doing this will help GC next time it roundtrips to know that each and all objects are ready for collecting. Also, you may not be looking correctly at the memory counter, even if Task Manager says your app is using 100 MB memory, these are just loose allocations, real usage could be anything from 1 to 100 MB.
 
Thanks for the reply.
Ive confirmened the memory usage with several sources. It is defitly accumulating out of control the more i manupulate my single tree.

Ive replace my dispose method with this one:
VB.NET:
Public Sub dispose() Implements IDisposable.Dispose
    If Not A Is Nothing Then A.dispose()
    If Not B Is Nothing Then B.dispose()
End Sub
which recursively descends the branch being free'd.
It doesn't really help.

After manipulating the tree and accumulating more that 100 megs of memory usage, I can call dispose upon the root tree instance and confirm that im working with a new tree. but memory still isnt being given up.

What other steps can i use to resolve this? what would you do if you were encountering this issue?

Thanks
 
define out-of-control?


The GC only runs when it feels like it. Dont be surprised if your vb.net program is "using" several hundred megs of memory the night before, and it's at 3 meg in the morning...

ps; you know the other thing is, if task manager didnt have those columns, or if you didnt attribute more meaning to them than they actually deserve, you wouldnt have wasted days trying to fix something that isnt broken. Move on, and only revisit this if your tree app crashes your machine. I doubt it will
 
Back
Top