473,796 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

garbage collection

Hello,

I have a c# application that creates a timer to fire every minute using
multithreading. On every minute it calls a Ping class that I made using
sockets.
It creates a new ping class then sets it to null when it’s done.

I noticed every time this class gets created I see the memory keeps getting
larger.
I think I should add Idisposable to my ping class and dispose of the memory
after the class is set to null.

Can someone give me their thoughts on this and maybe an example on how to
use the Idisposable class?
Thanks

Nov 17 '05
36 1938
skeet, what u have seen is, Socket destructor calls Dispose(true); this is
not a necessarity. the sample code explains everything.
http://msdn.microsoft.com/library/de...sposeTopic.asp
Nov 17 '05 #21
yes it seems interesting, but in respect to our discussion, finalizer or
desctructor does not call Dispose (at least destructor doesnt have to.).

one more interesting thing. compile and run that code. and see that it
definetly works!!


Nov 17 '05 #22
sorry for the second paragraph. you can ignore it.
Nov 17 '05 #23
The destructor is the finalizer. And the object will be destroyed when
Finalize() is called by the GC. Not before. Setting a field or variable to
null has no effect on the object it references. It is a pointer. You just
point it somewhere else. The object is unaffected.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"The Crow" <q> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
skeet, what u have seen is, Socket destructor calls Dispose(true); this is
not a necessarity. the sample code explains everything.
http://msdn.microsoft.com/library/de...sposeTopic.asp

Nov 17 '05 #24
Brian,

Right, the cases that the Finalizer doesn't run at all are rare, but you
would be supprised how many times I've seen it happen in scenarios where COM
interop is used on threads that are failing to pump the windows message
queue, or in Finalize methods that make blocking calls in unmanaged code
:-(.
Indeed, Constrained execution regions (CER's) are a great addition to the
framework, and a great tool in hosted environments.
Willy.
"Brian Gideon" <br*********@ya hoo.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
I believe those rare cases where the finalizer doesn't run at all can
be mitigated with the new constrained execution regions technology in
version 2. It is an interesting addition to the framework.

Brian

Willy Denoyette [MVP] wrote:
Yes, this is what I want to stress, while it's true that the Finalizer
thread kicks in on request of a GC run, there no strong guarantee that
your
Finalize (that calls your Dispose(bool)) method will execute at the next
Finalizer run, there are (very rare) cases in which your Finalizer might
never be called at all. That said, when you expect deterministic disposal
of
your unmanaged resources, you need an explicit call to Dispose (or use
the
using statement for fine grained scopes), relying on the Finalizer offers
you non-deterministic disposals.
Willy.

Nov 17 '05 #25
Kevin,
The Finalize method is called on ran on the "finalizer" thread (an high
priority thread) after the GC has done it's marks/sweep run, but this
decision is mabe by the EE not directly by the GC, and only when there are
finalizable objects in the "ready for finalization" queue, when no such
objects are queued, the finalizer thread is not started (why would it?).

Willy.

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:ey******** *****@TK2MSFTNG P10.phx.gbl...
The destructor is the finalizer. And the object will be destroyed when
Finalize() is called by the GC. Not before. Setting a field or variable to
null has no effect on the object it references. It is a pointer. You just
point it somewhere else. The object is unaffected.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.

"The Crow" <q> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
skeet, what u have seen is, Socket destructor calls Dispose(true); this
is not a necessarity. the sample code explains everything.
http://msdn.microsoft.com/library/de...sposeTopic.asp


Nov 17 '05 #26
Technically true, but you might as well say that an ignition key doesn't
cause exhaust fumes to exit the exhaust pipe of a car. It's a chain of
events that is all about Garbage Collection, and initialized by Garbage
Collection. In the context of this discussion, we're talking about objects
(Sockets) which WILL be finalized. The technical details are not
particularly relevant to this particular discussion. It all started with the
OP asking why his class was accumulating memory, and when told that he
needed to Dispose Sockets, he wanted to know why. Maybe I'm too concerned
with results. I do have my times of pure technological research, and it is
certainly worthwhile. However, those times don't occur when I'm trying to
debug something!

--
;-),

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Kevin,
The Finalize method is called on ran on the "finalizer" thread (an high
priority thread) after the GC has done it's marks/sweep run, but this
decision is mabe by the EE not directly by the GC, and only when there are
finalizable objects in the "ready for finalization" queue, when no such
objects are queued, the finalizer thread is not started (why would it?).

Willy.

"Kevin Spencer" <ke***@DIESPAMM ERSDIEtakempis. com> wrote in message
news:ey******** *****@TK2MSFTNG P10.phx.gbl...
The destructor is the finalizer. And the object will be destroyed when
Finalize() is called by the GC. Not before. Setting a field or variable
to null has no effect on the object it references. It is a pointer. You
just point it somewhere else. The object is unaffected.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Ambiguity has a certain quality to it.

"The Crow" <q> wrote in message
news:Ob******** ******@TK2MSFTN GP14.phx.gbl...
skeet, what u have seen is, Socket destructor calls Dispose(true); this
is not a necessarity. the sample code explains everything.
http://msdn.microsoft.com/library/de...sposeTopic.asp



Nov 17 '05 #27
i think you didnt read the code example. here it is :

using System;
using System.Componen tModel;

public class DisposeExample
{
public class MyResource: IDisposable
{
private IntPtr handle;
private Component component = new Component();
private bool disposed = false;
public MyResource(IntP tr handle)
{
this.handle = handle;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFina lize(this);
}

private void Dispose(bool disposing)
{
if(!this.dispos ed)
{
if(disposing)
{
component.Dispo se();
}

CloseHandle(han dle);
handle = IntPtr.Zero;
}
disposed = true;
}

[System.Runtime. InteropServices .DllImport("Ker nel32")]
private extern static Boolean CloseHandle(Int Ptr handle);

~MyResource()
{
Dispose(false); // This line is very important.... someone can
not include it.
}
}
public static void Main()
{
// Insert code here to create
// and use the MyResource object.
}
}
Nov 17 '05 #28
Hmm...I suppose it's me you are replying to, right?
When a class implement IDisposable it better adheres to the disposable
pattern, that is, a type that implements IDisposable should also have a
finalizer (called a destructor in C#), and that finalizer should call
Dispose(false).

But this is not my point, my point is that you should call dispose() when
you are done with the object if you want deterministic release of the
underlying resources, relying on finalize (your destructor ~) to call
Dispose(false) gives you non deterministic release, and should be avoided,
it's just a safety net to handle the cases that someone forgets to call
Dispose().

That's why I implement it as:

~MyResource()
{
Debug.Writeline ("BUG... you should call dispose instead of
relying on the finalizer");
Dispose(false);
}

I'm also not clear on what you mean by this:
// This line is very important.... someone can not include it. Guess you meant something like this:// This line is very important.... someone can not omit it.
Willy.

PS. Note the difference between Dispose() and Dispose(bool), in my replies?
the first must be called explicitely, the second is called by the finalize
methods (when the finalizer runs).
"The Crow" <q> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..i think you didnt read the code example. here it is :

using System;
using System.Componen tModel;

public class DisposeExample
{
public class MyResource: IDisposable
{
private IntPtr handle;
private Component component = new Component();
private bool disposed = false;
public MyResource(IntP tr handle)
{
this.handle = handle;
}

public void Dispose()
{
Dispose(true);
GC.SuppressFina lize(this);
}

private void Dispose(bool disposing)
{
if(!this.dispos ed)
{
if(disposing)
{
component.Dispo se();
}

CloseHandle(han dle);
handle = IntPtr.Zero;
}
disposed = true;
}

[System.Runtime. InteropServices .DllImport("Ker nel32")]
private extern static Boolean CloseHandle(Int Ptr handle);

~MyResource()
{
Dispose(false); // This line is very important.... someone can
not include it.
}
}
public static void Main()
{
// Insert code here to create
// and use the MyResource object.
}
}

Nov 17 '05 #29
yes. the whole point is "call dispose() when as soon as you done your job"
Nov 17 '05 #30

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

Similar topics

1
2338
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and then throws them away and then monitors the garbage collection and store statistics on it, preferably in C#. I want to know what is the longest period of time that an application may lock up while garbage collection is processing. Thanks!
6
810
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
2737
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind this? Is it because C is generally a 'low level' language and they didn't want garbage collection to creep into C++ and ruin everything? Just wondering :)
34
6435
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple, probably a few hundred lines of code in Python. There is a need to interact with network using the socket module, and then probably a need to do something hardware- related which will get its own driver written in C.
5
3614
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000 measurement exactly 10 ms apart. In the future this may decrease to 5ms ). I am concerned that if garbage collection invokes during this time it may interfere with our measurement results. I have looked over the garbage collection mechanism and see no...
8
3047
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by the mainline. The class that instantiated the object in question is definitely still in existence at the point garbage collection swoops in and yanks it out from under my processing. Is there a way to ensure an instantiated object cannot be freed...
28
3189
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will this work be library based or language based and will it be based on that of managed C++? Then of course there are the finer technical questions raised (especially due to pointer abuse). Is a GC for C++ just a pipe dream or is there a lot of work...
56
3716
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
350
11907
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
158
7909
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is discouraged due to some specific reason. If someone can give inputs on the same, it will be of great help. Regards, Pushpa
0
9528
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10006
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7547
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
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
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2925
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.