a few questions about Garbage Collector and IDisposable infce

This is a discussion on a few questions about Garbage Collector and IDisposable infce within the C# Programming forums, part of the General Programming Boards category; my understanding is that if you have an instance of a class that has members that occupy resources that you ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    a few questions about Garbage Collector and IDisposable infce

    my understanding is that if you have an instance of a class that has members that occupy resources that you want released after you are done working with the instance of the class, then you can have the class implement the IDisposable interface and then inside the implemented Dispose() method you can perform any clean up necessary. When such an instance of a class is declared and initialized inside a "using" block, the runtime automatically calls the Dispose() method on the instance of the class after the "using" block goes out of scope. Often, the Dispose() method will contain a line such as "GC.SuppressFinalize(this)" which tells the GC not to call the destructor method (which implicitly calls System.Object.Finalize()) on the instance of the class after the instance of the class goes out of scope.

    I don't understand exactly what the Finalize() or SupressFinalize methods do.

    I mean I understand that if you have reference type variables pointing toward a null reference then the data in memory that the reference type variables initially pointed to become available for garbage collection, as long as no other reference types point toward that data. Calling GC.Collect() will call the destructor method on unreferenced objects in memory.

    But I still don't get exactly what happens behind the scenes. In other words, if you have unreferenced objects in memory, then the next time the GC comes it will reclaim those memory resources allowing the occupied blocks of memory to be rewritten at a later time? And this reclaiming of memory resources happens the moment you call SuppressFinalize(this) or the moment the GC automatically calls the destructor method on unreferenced objects?

    and having the SuppressFinalize(this) method call inside the Dispose() method means that every time the Dispose() method is called, the memory occupied by the object will automatically be reclaimed? in other words, is the effect of doing SuppressFinalize(this) the same as doing "obj = null; GC.Collect();" minus the decrease in performance caused by explicitly calling GC.Collect()?
    Last edited by y99q; 10-17-2011 at 08:12 AM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    I'm not actually sure about the answer, but I believe your understanding of the GC is overly-simplified. The GC actually categorizes objects into different "generations". This is also assuming we're talking about the .NET Framework and not another environment like Mono, which might have a totally different GC heuristic.

    Anyway, these links might help:
    C# – Understanding The NET Garbage Collector..|..C# Help
    GC.SuppressFinalize Method (System)
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Garbage Collector
    By anirban in forum C++ Programming
    Replies: 4
    Last Post: 04-20-2010, 09:33 PM
  2. C++ Garbage Collector
    By audinue in forum C++ Programming
    Replies: 27
    Last Post: 08-16-2009, 07:20 AM
  3. Garbage Collector, Managed Heap etc.
    By mynickmynick in forum C Programming
    Replies: 2
    Last Post: 08-07-2008, 12:42 PM
  4. garbage collection
    By joed in forum C Programming
    Replies: 4
    Last Post: 04-01-2004, 12:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21