C# ping 类的内存泄露问题
pj220 2012-04-13 03:12:05 代码如下:
public class MyPing : Ping
{
public void PingCompletedCallBack(object sender, PingCompletedEventArgs e)
{
using (sender as Ping)
{
if (e.Reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", e.Reply.Address.ToString());
}
}
(sender as IDisposable).Dispose();
}
}
class Program
{
static void Main(string[] args)
{
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 1000;
for (int j = 0; j< 1000; j++)
{
for (int i = 0; i < 1000; i++)
{
MyPing pingSender = new MyPing();
try
{
pingSender.PingCompleted += new PingCompletedEventHandler(pingSender.PingCompletedCallBack);
pingSender.SendAsync(args[0], timeout, buffer, null);
}
catch ( Exception )
{
((IDisposable)pingSender).Dispose();
}
}
}
}
}
运行的系统为win7
现象是内存不断增长,最后出现内存耗尽,出现异常。
以下的这个帖子我已经看过,就是按照上面的说法使用了using 语句,可是问题依旧。
http://www.cnblogs.com/Gildor/archive/2009/09/08/1562225.html?login=1#commentform
在资源管理器中看多句柄数不断增长,可能还是内部有非托管资源没有回收导致的。
谁还有解决办法?