Question about Finalize & SuppressFinalize

  • Thread starter Thread starter Barry Anderberg
  • Start date Start date
B

Barry Anderberg

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.SuppressFinalize 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!
 
Barry,

You should also read up on the Dispose method pattern and the
IDisposable interface. Disposable classes typically call
SuppressFinalize when Dispose is called. You generally shouldn't call
SuppressFinalize 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
 
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.SuppressFinalize. As a
user, you should not be calling SuppressFinalize on Framework classes, you just need to properly use the Dispose pattern.

More infor ont he Dispose pattern:
http://msdn.microsoft.com/library/d.../en-us/cpgenref/html/cpconfinalizedispose.asp

Hope that helps
-Chris


--------------------
From: (e-mail address removed) (Barry Anderberg)
Newsgroups: microsoft.public.dotnet.general
Subject: Question about Finalize & SuppressFinalize
Date: 10 May 2004 13:19:35 -0700
Organization: http://groups.google.com
Lines: 23
Message-ID: <[email protected]>
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: (e-mail address removed)
NNTP-Posting-Date: Mon, 10 May 2004 20:19:36 +0000 (UTC)
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.com!newsfeed.icl.net!proxad.net!209.98.3.200.MISMATCH! priapus.visi.com!orange.octanews.net!news.octanews.net!green.octanews.net!news-out.octanews.net!news.glorb.com!postnews1.google.com!not-for-mail
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.general:133629
X-Tomcat-NG: microsoft.public.dotnet.general

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.SuppressFinalize 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!


--

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
http://www.microsoft.com/info/cpyright.htm

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.
 
Back
Top