Results 1 to 4 of 4

Thread: [2005] Checking for memory leaks and disposing of objects

  1. #1

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    [2005] Checking for memory leaks and disposing of objects

    Hi there,

    I left my program running in debug mode all night and found that by this morning all my memory had been consumed by it (2GB for a tiny little program is a little excessive, methinks). As I am fairly new to VB.Net and just as new to programming on the Windows platform, I am unsure what to use or how to go about it. Under Linux I would use valgrind and/or electric fence.
    I know that in my code I do New calls, which I was under the impression would be disposed of by the garbage collector as soon as the var goes out of scope or no references are left to the object in question. Are unused objects something I should take care of myself? I don't have a problem with that, plenty of other languages require you to do that for you. What would be the preferred method to get rid of an unused object? Would it be foo.Dispose() or foo = Nothing? I noticed that not all classes have the Dispose() method either.
    My program is rather small at the moment, so tracking down all the New calls by hand would be fairly trivial. However, I'd like to learn how to deal with this kind of problem in a generic way so that as my program grows, I will still be able to apply the same methods to track down the root cause (apart from the obvious: bad programming).

    Thanks,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Checking for memory leaks and disposing of objects

    Resource management is very easy in .NET applications. There are really only two things to remember:

    1. Dispose every object that supports it when you're finished with it. You should employ Using blocks wherever possible to facilitate this.

    2. If you have finished with a reference type object and a variable that refers to it does not, or may not, lose scope for some time then set that variable to Nothing.

    Note that disposing objects and setting variables to Nothing are two completely different things. Disposing an object does NOT release any memory, nor does it even make any memory available to be reclaimed. Disposing an object releases unmanaged resources, e.g. file handles or window handles. A disposed object still exists and still occupies memory. In fact, a disposed object could exists indefinitely.

    Setting a variable to Nothing does not directly affect an object. If there are other existing references to an object then setting a variable to Nothing has no useful effect. When you set a variable to Nothing you remove that reference to the object. If that was the last reference to the object then it is flagged as available for cleanup. What that means is that at some time in the future the garbage collector can reclaim the memory occupied by that object.

    If a variable loses scope soon after then setting it to Nothing serves no useful purpose because the reference is removed once the variable loses scope anyway.

    Note that if you remove all references to an object without disposing it then that object is lost to you and you will be unable to dispose it in the future. This is a bad thing because it means that, when the GC comes to cleanup that object, it must first dispose it and then wait until the next pass to clean it up.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member mightor's Avatar
    Join Date
    Apr 2007
    Location
    Turn around, I'm right behind you...
    Posts
    99

    Re: [2005] Checking for memory leaks and disposing of objects

    Thanks for the explanation.
    Basically there is no explicit way to cause a newly created object to be released back to memory? How often does the GC run?
    Is there a way to check for memory leaks with VB 2005 Pro and if so, how?
    I read somewhere (I cannot remember where that was, sorry) that despite the ByVal keyword in a function prototype, some arguments will still be passed ByRef. Is this is ever the other way around? How is this determined? This must have repercussions for the cleanup of newly created objects, no?

    Gr,
    Mightor
    What the world needs is more geniuses with humility, there are so few of us left.
    If my post was accidentally useful, please rate it as such, thanks!
    Mon aéroglisseur est plein des anguilles.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Checking for memory leaks and disposing of objects

    The GC runs whenever it's needed. If you have just finished with a large block of memory then you can force the GC to run but this is generally not required and actually counter-productive.
    I read somewhere (I cannot remember where that was, sorry) that despite the ByVal keyword in a function prototype, some arguments will still be passed ByRef.
    Whoever told you that has no idea what they're talking about. Arguments are always passed by value or by reference exactly as you specify. Passing arguments by value or by reference refers to the variable that you pass. If you pass by value then the value of the variable is passed. If you pass by reference then a reference to the variable is passed. When you pass a variable by value it means that any changes you make to that variable will not stick after the method call. If you pass the variable by reference the changes will stick. Note though that a reference type variable contains a reference, not a value. That means that if you pass a reference type variable by value then if you assign a new object to the variable then that is the change that will not stick, but if you make any changes to the object that the variable refers to the will stick, whether you pass that variable by value or by reference. This makes perfect sense, but can confuse those who don't really understand the difference between value types and reference types.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width