473,563 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GC.SuppressFina lize?

Hi NG,

I am not sure to evaluate what happens if I call the
GC.SuppressFina lize(this) for a object.
My question is:
In case I call GC.SuppressFina lize(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<stringstrL ist;
public TestObject()
{
strList = new List<string>();
}

public void Dispose()
{
<releas my hw>
GC.GC.SuppressF inalize(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 5816
Rainer Queck wrote:
I am not sure to evaluate what happens if I call the
GC.SuppressFina lize(this) for a object.
My question is:
In case I call GC.SuppressFina lize(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<stringstrL ist;
public TestObject()
{
strList = new List<string>();
}

public void Dispose()
{
<releas my hw>
GC.GC.SuppressF inalize(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.SuppressFina lize(this) for a object.
My question is:
In case I call GC.SuppressFina lize(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<stringstrL ist;
public TestObject()
{
strList = new List<string>();
}
public void Dispose()
{
<releas my hw>
GC.GC.SuppressF inalize(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.SuppressFina lize in the code that you posted. GC.SuppressFina lize(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.SuppressFina lize
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.SuppressFina lize 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 DisposableObjec t: IDisposable
{
private enum DisposeState { NotDisposed, Disposing, Disposed }

private DisposeState m_State = DisposeState.No tDisposed;
private object m_StateLock = new object();

~DisposableObje ct() // overrides Object.Finalize ()
{
InnerDispose(fa lse);
}

private void InnerDispose(bo ol disposing)
{
lock (m_StateLock)
{
if (m_State != DisposeState.No tDisposed)
return;

m_State = DisposeState.Di sposing;
try
{
Dispose(disposi ng);
}
finally
{
m_State = DisposeState.Di sposed;
}
}
}

protected abstract void Dispose(bool disposing);

public void Dispose()
{
InnerDispose(tr ue);
GC.SuppressFina lize(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.Dis pose() 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.Dis pose() 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<stringobje ct 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
2064
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 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...
2
830
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: 'SuppressFinalize' : is not a member of 'System::GC' : see declaration of 'System::GC'. This diagnostic occurred in the compiler generated function...
8
1691
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 dialog mode. ' button code ... Dim frm As New frmAbout frm.ShowDialog(Me) frm.Dispose()
3
6300
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 FileSteam, who is trying to flush and close the stream, but the disk is full, so the flush fails and another exception is thrown from another (GC)...
3
4493
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 implementation, and I could not find a way for my child type to override parent's IDisposable.Dispose() method. The intention was to clean up...
6
6339
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 instance itself, for example in the following code: public class Test1 : IDisposable { public string Hello() { return "HEllo"; }
8
8980
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 http://www.codeproject.com/managedcpp/garbage_collection.asp - sources
0
951
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 GC.SuppressFinalize(this); in the same method?
1
3355
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 in the debug output that the client connects to the server, the events get fired and the disconnect event gets fired as well. I get confirmation...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7948
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.