473,785 Members | 2,801 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 #1
36 1937
Hi Andre,

You might want to take a look at the following SDK topic:

http://msdn.microsoft.com/library/de...classtopic.asp

IDisposable is not a class; it's an Interface. And if your class employs any
Disposable classes, or interacts with any unmanged or marshalled resources,
setting it to null doesn't do much of anything. Read the article for
details.

--
HTH,

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

"André" <An**@discussio ns.microsoft.co m> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
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 #2
Thanks Kevin,

I only use managed code. I am using the sockets class in C#

Does this mean I should not use the Idiposable interaface?

"Kevin Spencer" wrote:
Hi Andre,

You might want to take a look at the following SDK topic:

http://msdn.microsoft.com/library/de...classtopic.asp

IDisposable is not a class; it's an Interface. And if your class employs any
Disposable classes, or interacts with any unmanged or marshalled resources,
setting it to null doesn't do much of anything. Read the article for
details.

--
HTH,

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

"André" <An**@discussio ns.microsoft.co m> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
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 #3
The System.Net.Sock ets.Socket class implements IDisposable. This indicates
that a Socket should be disposed when you're through with it. Since your
class implements at least one Socket, it should ensure that the Socket(s) is
disposed when your class is finished with it.

This means that your class should employ structured exception handling and
other techniques to ensure that this happens. If the Socket is a publicly
exposed member of your class, implementing IDisposable could be a very good
idea, to ensure that any developer implementing your class does not forget
to Dispose the Socket. Your Dispose method would Dispose the Socket.

Classes that implement IDisposable are eventually Garbage-collected even if
they are not Disposed, but there is a significant delay in the release of
the unmanaged resources they use, which can cause the type of memory
build-up you observed.

--
HTH,

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

"André" <An**@discussio ns.microsoft.co m> wrote in message
news:E4******** *************** ***********@mic rosoft.com...
Thanks Kevin,

I only use managed code. I am using the sockets class in C#

Does this mean I should not use the Idiposable interaface?

"Kevin Spencer" wrote:
Hi Andre,

You might want to take a look at the following SDK topic:

http://msdn.microsoft.com/library/de...classtopic.asp

IDisposable is not a class; it's an Interface. And if your class employs
any
Disposable classes, or interacts with any unmanged or marshalled
resources,
setting it to null doesn't do much of anything. Read the article for
details.

--
HTH,

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

"André" <An**@discussio ns.microsoft.co m> wrote in message
news:DD******** *************** ***********@mic rosoft.com...
> 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 #4
Thanks Kevin,

Can you help me add the dispose interface?

This is my simple ping class. I looked at the example on Microsoft’s web
page, but not sure how to clean up the resources.

I have one class calling the ping (keepalive) class that calls it like this
every minute.

private void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{

KeepAlive keep = new KeepAlive(Conve rt.ToInt32(Loca lPort));
keep.ping();
keep = null;
}
//*************** *************** *************

//Calls this class.

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sock ets;

namespace SSH2
{
public class KeepAlive
{
int _port;
internal KeepAlive(int port)
{
_port = port;
}

internal void ping()
{
try
{
Encoding ASCII = Encoding.ASCII;
IPEndPoint hostEndPoint;
IPAddress hostAddress = System.Net.IPAd dress.Loopback;
int conPort = port;
string Get = "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nCo nnection:
Close\r\n\r\n";

Byte[] ByteGet = ASCII.GetBytes( Get);
byte[] RecvBytes = new byte[256];
hostEndPoint = new IPEndPoint(host Address, conPort);
Socket s= new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
s.Connect(hostE ndPoint);
s.Send(ByteGet, ByteGet.Length, 0);

s.Close();
}
catch
{
}

}

internal int port
{
get
{
return _port;
}
set
{
_port = value;
}
}
}
}

Nov 17 '05 #5
André,

In this particular case KeepAlive does not need to implement
IDisposable. This is because Socket is only used in the ping method.
With structured exception handling you can be sure that the Socket
always gets disposed inside that method. To make things easier use the
Socket inside a using block. This guarentees that Dispose will be
called on the Socket even if an exception occurs.

using (Socket s = new Socket(...))
{
s.Connect(...);
s.Send(...);
}
Brian

André wrote:
Thanks Kevin,

Can you help me add the dispose interface?

This is my simple ping class. I looked at the example on Microsoft's web
page, but not sure how to clean up the resources.

I have one class calling the ping (keepalive) class that calls it like this
every minute.

private void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{

KeepAlive keep = new KeepAlive(Conve rt.ToInt32(Loca lPort));
keep.ping();
keep = null;
}
//*************** *************** *************

//Calls this class.

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sock ets;

namespace SSH2
{
public class KeepAlive
{
int _port;
internal KeepAlive(int port)
{
_port = port;
}

internal void ping()
{
try
{
Encoding ASCII = Encoding.ASCII;
IPEndPoint hostEndPoint;
IPAddress hostAddress = System.Net.IPAd dress.Loopback;
int conPort = port;
string Get = "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nCo nnection:
Close\r\n\r\n";

Byte[] ByteGet = ASCII.GetBytes( Get);
byte[] RecvBytes = new byte[256];
hostEndPoint = new IPEndPoint(host Address, conPort);
Socket s= new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
s.Connect(hostE ndPoint);
s.Send(ByteGet, ByteGet.Length, 0);

s.Close();
}
catch
{
}

}

internal int port
{
get
{
return _port;
}
set
{
_port = value;
}
}
}


}


Nov 17 '05 #6
On Tue, 18 Oct 2005 08:39:04 -0700, André
<An**@discussio ns.microsoft.co m> wrote:
Thanks Kevin,

Can you help me add the dispose interface?


The code you posted wouldn't be helped by the IDisposable interface
since the socket isn't a persistant resource.

I do have a couple of remarks about your code, including an unmanaged
resource leak. I made some changes to your code to make it cleaner.
Comments are inline:

internal void ping()
{
// Declaration need to be before try block since
// variable is used in finally block
Socket s = null;

try
{
Encoding ASCII = Encoding.ASCII;
IPEndPoint hostEndPoint;
IPAddress hostAddress = System.Net.IPAd dress.Loopback;
int conPort = port;
string Get = "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nCo nnection:
Close\r\n\r\n";
Byte[] ByteGet = ASCII.GetBytes( Get);
byte[] RecvBytes = new byte[256];
hostEndPoint = new IPEndPoint(host Address, conPort);
Socket s= new Socket(AddressF amily.InterNetw ork,
SocketType.Stre am, ProtocolType.Tc p);
s.Connect(hostE ndPoint);
s.Send(ByteGet, ByteGet.Length, 0);
}
catch(SocketExc eption) // Catch specific exceptions. Swallowing
// all exceptions are bad.
{

}
finally
{
// Everything in finally is called even if an exception has been
// thrown.

// The previous version would leak an unmanaged socket
// resource if an exception was thrown before s.Close() was
// called.

// Call s.Close() or s.Dispose() to release the socket.
// I personally prefer Dispose since it is more generic to the
// framework.
if(s!=null)
s.Dispose();
}
}

--
Marcus Andrén
Nov 17 '05 #7
ok, i've made the change. Do i just leave this code as it stands?

private void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{

KeepAlive keep = new KeepAlive(Conve rt.ToInt32(Loca lPort));
keep.ping();
keep = null;
}
"Brian Gideon" wrote:
André,

In this particular case KeepAlive does not need to implement
IDisposable. This is because Socket is only used in the ping method.
With structured exception handling you can be sure that the Socket
always gets disposed inside that method. To make things easier use the
Socket inside a using block. This guarentees that Dispose will be
called on the Socket even if an exception occurs.

using (Socket s = new Socket(...))
{
s.Connect(...);
s.Send(...);
}
Brian

André wrote:
Thanks Kevin,

Can you help me add the dispose interface?

This is my simple ping class. I looked at the example on Microsoft's web
page, but not sure how to clean up the resources.

I have one class calling the ping (keepalive) class that calls it like this
every minute.

private void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{

KeepAlive keep = new KeepAlive(Conve rt.ToInt32(Loca lPort));
keep.ping();
keep = null;
}
//*************** *************** *************

//Calls this class.

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sock ets;

namespace SSH2
{
public class KeepAlive
{
int _port;
internal KeepAlive(int port)
{
_port = port;
}

internal void ping()
{
try
{
Encoding ASCII = Encoding.ASCII;
IPEndPoint hostEndPoint;
IPAddress hostAddress = System.Net.IPAd dress.Loopback;
int conPort = port;
string Get = "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nCo nnection:
Close\r\n\r\n";

Byte[] ByteGet = ASCII.GetBytes( Get);
byte[] RecvBytes = new byte[256];
hostEndPoint = new IPEndPoint(host Address, conPort);
Socket s= new Socket(AddressF amily.InterNetw ork, SocketType.Stre am,
ProtocolType.Tc p);
s.Connect(hostE ndPoint);
s.Send(ByteGet, ByteGet.Length, 0);

s.Close();
}
catch
{
}

}

internal int port
{
get
{
return _port;
}
set
{
_port = value;
}
}
}
}


Nov 17 '05 #8
Yes, that's fine. Except, you really don't need to set the object
reference to null.

André wrote:
ok, i've made the change. Do i just leave this code as it stands?

private void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{

KeepAlive keep = new KeepAlive(Conve rt.ToInt32(Loca lPort));
keep.ping();
keep = null;
}


Nov 17 '05 #9
I still notice that the memory usage is getting higher each time the ping is
created. I know that the Garbage collection will get to it when it does.
Should I call the GC.collect? or just let it be?

Also, when is a time that I should use the IDisposable interface? I’m still
new to GC

Thanks
"Brian Gideon" wrote:
Yes, that's fine. Except, you really don't need to set the object
reference to null.

André wrote:
ok, i've made the change. Do i just leave this code as it stands?

private void timer_Elapsed(o bject sender, System.Timers.E lapsedEventArgs e)
{

KeepAlive keep = new KeepAlive(Conve rt.ToInt32(Loca lPort));
keep.ping();
keep = null;
}


Nov 17 '05 #10

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
6434
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
3613
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
3188
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
3715
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
11893
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
7906
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
10336
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9953
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
7502
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
6741
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
5383
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...
1
4054
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
3
2881
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.