473,387 Members | 1,303 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,387 software developers and data experts.

GC.SuppressFinalize?

Hi NG,

I am not sure to evaluate what happens if I call the
GC.SuppressFinalize(this) for a object.
My question is:
In case I call GC.SuppressFinalize(this) from with in one of my object, do I
then have to somehow - if yes how - release instances created in the
constructor manuelly?

Simplified example:

public class TestObject
{
private List<stringstrList;
public TestObject()
{
strList = new List<string>();
}

public void Dispose()
{
<releas my hw>
GC.GC.SuppressFinalize(this);
}
}

Would this code mean, that I have to "release" strList within my dispose or
will GC still take care of strList?

Note : I know, that this way of handling dispose is not complete.
I just extracted some code from the "DisposeExample" from
http://msdn.microsoft.com/library/de...alizetopic.asp

Regards
Rainer

Dec 7 '06 #1
6 5812
Rainer Queck wrote:
I am not sure to evaluate what happens if I call the
GC.SuppressFinalize(this) for a object.
My question is:
In case I call GC.SuppressFinalize(this) from with in one of my object,
do I then have to somehow - if yes how - release instances created in the
constructor manuelly?

Simplified example:

public class TestObject
{
private List<stringstrList;
public TestObject()
{
strList = new List<string>();
}

public void Dispose()
{
<releas my hw>
GC.GC.SuppressFinalize(this);
}
}

Would this code mean, that I have to "release" strList within my dispose
or will GC still take care of strList?

Note : I know, that this way of handling dispose is not complete.
I just extracted some code from the "DisposeExample" from
http://msdn.microsoft.com/library/de...alizetopic.asp
The point of this type of dispose implementation is that you have taken care
to free any resources that need to be freed in your Dispose method and
therefore there is no need to call your Finalizer. That doesn't mean you
shouldn't code a Finalizer that also releases any resources that you are
holding (ideally by calling the same method that your public Dispose calls
but with a switch to indicate where it is being called from), but it
indicates to the garbage collector that it does not need to explicitly call
your Finalize since your internal resources have already been released by
your Dispose method. Having a Finalize method gives the GC the chance to
get into your resource freeing method in the event that someone forgets to
call your Dispose method.

In your specific example, I don't think there is anything you need to do, so
would question why you are implementing a dispose pattern at all. Your
List<stringwill be handled by the GC as those are managed resources that
don't have to be taken care of by you (what are you going to do in your
Dispose with the strList to release those resosurce?).

Implementing IDisposable when there is no reason to do so usually isn't good
practice.
--
Tom Porterfield

Dec 7 '06 #2
I am not sure to evaluate what happens if I call the
GC.SuppressFinalize(this) for a object.
My question is:
In case I call GC.SuppressFinalize(this) from with in one of my
object, do I
then have to somehow - if yes how - release instances created in the
constructor manuelly?
Simplified example:

public class TestObject
{
private List<stringstrList;
public TestObject()
{
strList = new List<string>();
}
public void Dispose()
{
<releas my hw>
GC.GC.SuppressFinalize(this);
}
}
Would this code mean, that I have to "release" strList within my
dispose or will GC still take care of strList?
GC will still take care of strList. The only difference is that TestObject's
Finalize method won't be called. And, since TestObject doesn't actually override
the Object.Finalize method (using the destructor syntax), there's no need
to call GC.SuppressFinalize in the code that you posted. GC.SuppressFinalize(this)
will essentially do nothing in the code you posted.

Here's what happens when you override the Finalize method (or in C#, declare
a destructor). This will help explain the situation where calling GC.SuppressFinalize
is necessary.

When an object is allocated that overrides the Finalize method, a pointer
to the object is placed in a special 'finalization' queue. When the garbage
collector sees that the object can be reclaimed, it scans the finalization
queue and finds that a pointer to the object exists in the queue. The GC
then removes the pointer from the 'finalization' queue and adds it to the
'freachable' queue. The object actually survives the GC because the Finalize
method hasn't been called yet. There is a special thread in the runtime for
calling Finalize methods. When a pointer appears in the freachable queue,
this thread wakes up and calls the Finalize methods of the objects in the
queue. After this is done, the freachable queue is empty and the next GC
will reclaim the object (unless the object is resurrected in some way by
its Finalize method).

The net result of all of this is that, relying on the Finalize method can
have some unwanted side effects -- not the least of which is performance.
Enter the GC.SuppressFinalize method. When this is called, it ensures that
the Finalize method of the specified object is not called. So, the object
will be reclaimed on GC and not moved to the freachable queue. This makes
it particularly when implementing IDisposable.

FWIW, here is an abstract class that I use as a base IDisposable implementation:

public abstract class DisposableObject: IDisposable
{
private enum DisposeState { NotDisposed, Disposing, Disposed }

private DisposeState m_State = DisposeState.NotDisposed;
private object m_StateLock = new object();

~DisposableObject() // overrides Object.Finalize()
{
InnerDispose(false);
}

private void InnerDispose(bool disposing)
{
lock (m_StateLock)
{
if (m_State != DisposeState.NotDisposed)
return;

m_State = DisposeState.Disposing;
try
{
Dispose(disposing);
}
finally
{
m_State = DisposeState.Disposed;
}
}
}

protected abstract void Dispose(bool disposing);

public void Dispose()
{
InnerDispose(true);
GC.SuppressFinalize(this); // Finalize method will no longer be called.
}
}

Note that this class ensures that two threads cannot cause the object to
be disposed more than once. When overridding the Dispose(bool disposing)
method, make sure to check the 'disposing' parameter. If this is true, it
is called on the same thread that Dispose() was called on. If it is false,
it is being called from the runtime's finalizer thread that calls Finalize
methods.

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 7 '06 #3
BTW, I recommend checking out this article for the nuts and bolts:

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

Best Regards,
Dustin Campbell
Developer Express Inc.
Dec 7 '06 #4
Hi Rainer,

Since you are implementing Dispose() method for your class, I assume you
are implementing IDisposable.Dispose() method.

IDisposable interface is only used to explicitly release unmanaged resource
such as file handle, bitmap resource or other OS unmanaged resource. This
is because .Net knows nothing about unmanaged resource in your class, you
should implement IDisposable.Dispose() method to free the unmanaged
resource. However, it is not used to dispose managed resource such as .Net
object memory because .Net GC will take care of releasing it.

In your scenario, if you are only concerning List<stringobject in your
class, there is no need to implement IDisposable interface for it.

The article below talks more about when and how to use Dispose in C#:
"When and How to Use Dispose and Finalize in C#"
http://www.devx.com/dotnet/Article/33167/1954?pf=true

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 8 '06 #5
Hi Tom, Mark, Dustin and Jeffrey,

thanks for your hints and information.

Good to hear, that GC still takes care of my strList so that I can use the
Dispose pattern as basically shown in my example.
I do need this pattern since <my hardwareis a unmanaged recource I have to
take care of.

Regards
Rainer
Dec 8 '06 #6
Hi Rainer,

Glad to hear our replies make sense to you. If you need further information
regarding the GC topic, the best resource is the articles from "Jeffrey
Richter":
"Garbage Collection: Automatic Memory Management in the Microsoft .NET
Framework"
http://msdn.microsoft.com/msdnmag/issues/1100/gci/

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Dec 11 '06 #7

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

Similar topics

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...
2
by: sunrat | last post by:
After installing .NET Framework SP1, I find that re-compiling Visual C++.NET code with Visual Studio 2003 (v 7.1.3088) that previously compiled successfully gives rise to two errors: C2039:...
8
by: Joăo Santa Bárbara | last post by:
Hi all, lets get start. i have a few questions to ask perhaps someone can help me. i´m doing a simple aplication. some thing like this i have a form with a button that opens another form in...
3
by: Muki Rapp | last post by:
Hi! In the example below, once the media is full, the FileSteam.WriteByte throws an exception and the code is designed to handle it. However, when the GC is invoked, it calls the Finalize of...
3
by: Ivan Neganov | last post by:
Hi, I have a custom subclass of System.IO.Stream type. I wonder how to correctly implement the IDisposable pattern in this situation. The parent Stream type apparently uses explicit interface...
6
by: Bob | last post by:
When implementing the Dispose() method, I understand that it's supposed to do what Finalize() does so that's why it should call GC.SuppressFinalize(). However, it's not clear to me what happens the...
8
by: Varangian | last post by:
Hello, was wondering of how to dispose of managed resources? or referencing every member of a class to null will release resources...? http://www.marcclifton.com/tabid/79/Default.aspx...
0
by: cksrajapplabs | last post by:
I am using User Controls in my Win Forms. How to dispose those controls by using Dispose() method? I am calling base.Dispose(); method in the Dispose() method. Is it must to call...
1
Airslash
by: Airslash | last post by:
Hello, The problem is that my server is not receiving data. The code below are the various classes I designed around sockets. It will be big... I have run the code with the debugger, and I see...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.