Results 1 to 8 of 8

Thread: Garbage Collection

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Garbage Collection

    I've been wondering about something now that I've done a bit in .NET. How reliable is the garbage collection?

    In a sub, I could create a command, a dataset, a datareader, a new form, etc. Then at the end of the sub, what happens if I do not dispose of them? In C++ (unmanaged) you get a memory leak if you use New to acquire memory, and fail to free it up after you are done with it. New doesn't seem to serve the same purpose in .NET, but in some cases it serves a similar purpose.

    Is it simply good practice to dispose of locally declared (dimmed) variables, or is it necessary to avoid memory leaks?
    My usual boring signature: Nothing

  2. #2
    Addicted Member
    Join Date
    Sep 2004
    Location
    Brooklyn
    Posts
    147
    Some people manually .Dispose() everything, no matter how small. I don't, even though my apps tend to stay open on the user's desktop all day long. I haven't noticed a memory leak problem ever, and about half of my user's desktops have only 64 or 128mb of RAM - the rest have 256.

  3. #3

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    The issue I run into is that if I were to create a command and then try to create a datareader both within a Try block, and an exception gets thrown.

    At that point, there are a couple of things that could have thrown exceptions.

    One option would be to try to figure out from the type of exception exactly where the exception was thrown, and determine from that whether or not the command needs to be disposed, but that seems to be an incomplete solution, because that would require that you deal with every possible exception that could arise. I don't believe I have that list.

    Another option, and one I have used, would be to check to see if the component is nothing, and dispose of it if need be in a Finally block.

    A third option is to handle each risky step within it's own Try block, but that has other issues.

    What I'd like to do is just to ignore locally declared variables, and let them be cleaned up as they go out of scope, but I'm not sure how such a practice would be received, and whether or not it is safe.
    My usual boring signature: Nothing

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    GC is safe and reliable in .NET.

    Read this:

    http://msdn.microsoft.com/msdnmag/issues/1100/GCI/

    I usually declare my objects outside the Try Catch block, btw. Insantiation within or outside.
    Main potentionally-error-prone operations within the Try Catch block.
    Clean up in the Finally area, so that I get rid of them anyways.

    Legal.

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Memory leaks are a thing of the past apparently.
    I don't live here any more.

  6. #6

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Well then, I may just get a bit lazy.

    Is there ever a time in .NET when you cannot have something throw an error? You could put a Try...Catch within a Catch block to trap an error, but is there a place where you can't live with an error?

    It seems like the problems with constructors throwing errors that exist in C++ would also apply to VB.NET. Should you utterly avoid having the sub New raise an error?
    My usual boring signature: Nothing

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    in the sticky at the top of the forum the 101 vb.net samples has a garbage collection example.

    BTW, objects that implement a dispose method, do so because the GC doesn't automatically reclaim the resources used by these objects, therefor once you are done with them you should ALWAYS call dispose

  8. #8

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Bummer. Just what I didn't want....a voice of reason

    Ok, I guess that's what I needed to hear.
    My usual boring signature: Nothing

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