473,394 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.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!
Jul 21 '05 #1
2 2047
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
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/de...izedispose.asp

Hope that helps
-Chris
--------------------
From: ba******@yahoo.com (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: <9d**************************@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: gr**********@google.com
NNTP-Posting-Date: Mon, 10 May 2004 20:19:36 +0000 (UTC)
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFT NGP08.phx.gbl!news-out.cwix.com!newsfeed.cwix.com!newsfeed.icl.net!pr oxad.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.c om!not-for-mailXref: 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.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: Shaun McKinnon | last post by:
HI...Here's my problem...I have a popup window that loads when i want it to, but it's not sized properly. I've set the size, but it doesn't seem to work. I've been on 8 different websites to find...
20
by: Charles Law | last post by:
I have an application that creates a class. The class has unmanaged resources, so must end gracefully. How can I guarantee that the unmanaged resources are freed? I have looked at IDisposable,...
3
by: Boni | last post by:
Dear all, can somebody explain difference between Dispose and Finalize and when each of them should be used? Thank you very much, Boni
2
by: Barry Anderberg | last post by:
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...
4
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue,...
1
by: Sagaert Johan | last post by:
Hi If my destructor of my class (derived from usercontrol ) never gets called , does this mean that its not collected by the GC and hence allocated memory is not freed ? Is there a way to...
2
by: Workarounder | last post by:
Hi! I have a program and in this program I have to do a lot of close´s and free´s. The program is something like this: static int finalize(FILE* f1, FILE* f2, char* c) { if (f1 != NULL) { ...
15
by: Barry Flynn | last post by:
VB 2005. I have the following code in a Sub. Dim oFred As SillyClass oFred = New SillyClass oFred.Gloop() oFred = Nothing Exit Sub
9
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've a question regarding a finalizer. Why is it that the finalize method of a form is never being called? I create a simple program... public partial class Form1 :...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.