Question about Finalize & SuppressFinalize

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Barry Anderberg

    Question about Finalize & SuppressFinalize

    I've been doing some reading about Finalize and garbage collection.
    I've learned that finalizing should be avoided because objects that
    have a finalize method require 2 (possibly more) itterations of the
    garbage collector to run before the memory is returned to the heap.
    The first time the GC runs the Finalize method is called, then the
    second time the memory is actually freed.

    The problem is that most of the major classes in the .NET framework
    have a Finalize method.

    The System.Windows. Forms.Control and System.Windows. Forms.Form classes
    BOTH have finalize methods.

    Am I to understand that I should always call GC.SuppressFina lize after
    creating these objects?

    Otherwise from what I have read it will take two or MORE garbage
    collection cycles to free the memory!!

    This seems wrong. Why would MS do this? I don't think they would
    which makes me think there's some part of the picture I don't get.

    Help!
  • Mattias Sjögren

    #2
    Re: Question about Finalize & SuppressFinaliz e

    Barry,

    You should also read up on the Dispose method pattern and the
    IDisposable interface. Disposable classes typically call
    SuppressFinaliz e when Dispose is called. You generally shouldn't call
    SuppressFinaliz e for objects other than your own.

    Forms are disposed when closed, and they ensure that all constrols on
    the form are disposed as well.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Chris Lyon [MSFT]

      #3
      RE: Question about Finalize & SuppressFinaliz e

      Hi Barry

      Most of the classes in the .NET Framework that have finalizers also implement the IDisposable interface, and inside the Dispose method is a call to GC.SuppressFina lize. As a
      user, you should not be calling SuppressFinaliz e on Framework classes, you just need to properly use the Dispose pattern.

      More infor ont he Dispose pattern:


      Hope that helps
      -Chris


      --------------------[color=blue]
      >From: banderbe@yahoo. com (Barry Anderberg)
      >Newsgroups: microsoft.publi c.dotnet.genera l
      >Subject: Question about Finalize & SuppressFinaliz e
      >Date: 10 May 2004 13:19:35 -0700
      >Organization : http://groups.google.com
      >Lines: 23
      >Message-ID: <9dd925b8.04051 01219.672f720e@ posting.google. com>
      >NNTP-Posting-Host: 65.214.110.254
      >Content-Type: text/plain; charset=ISO-8859-1
      >Content-Transfer-Encoding: 8bit
      >X-Trace: posting.google. com 1084220376 14778 127.0.0.1 (10 May 2004 20:19:36 GMT)
      >X-Complaints-To: groups-abuse@google.co m
      >NNTP-Posting-Date: Mon, 10 May 2004 20:19:36 +0000 (UTC)
      >Path: cpmsftngxa10.ph x.gbl!TK2MSFTFE ED01.phx.gbl!TK 2MSFTNGP08.phx. gbl!news-out.cwix.com!ne wsfeed.cwix.com !newsfeed.icl.n et!proxad.net!2 09.98.3.200.MIS MATCH![/color]
      priapus.visi.co m!orange.octane ws.net!news.oct anews.net!green .octanews.net!n ews-out.octanews.ne t!news.glorb.co m!postnews1.goo gle.com!not-for-mail[color=blue]
      >Xref: cpmsftngxa10.ph x.gbl microsoft.publi c.dotnet.genera l:133629
      >X-Tomcat-NG: microsoft.publi c.dotnet.genera l
      >
      >I've been doing some reading about Finalize and garbage collection.
      >I've learned that finalizing should be avoided because objects that
      >have a finalize method require 2 (possibly more) itterations of the
      >garbage collector to run before the memory is returned to the heap.
      >The first time the GC runs the Finalize method is called, then the
      >second time the memory is actually freed.
      >
      >The problem is that most of the major classes in the .NET framework
      >have a Finalize method.
      >
      >The System.Windows. Forms.Control and System.Windows. Forms.Form classes
      >BOTH have finalize methods.
      >
      >Am I to understand that I should always call GC.SuppressFina lize after
      >creating these objects?
      >
      >Otherwise from what I have read it will take two or MORE garbage
      >collection cycles to free the memory!!
      >
      >This seems wrong. Why would MS do this? I don't think they would
      >which makes me think there's some part of the picture I don't get.
      >
      >Help!
      >[/color]


      --

      This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
      Use these online forms to report copyright and trademark infringement to Microsoft Legal. Infringement notices must comply with the Digital Millennium Copyright Act.


      Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

      Comment

      Working...