什么是如果我不调用UdpClient.Close()方法的缺点? [英] What is the drawback if I do not invoke the UdpClient.Close() method?

查看:1181
本文介绍了什么是如果我不调用UdpClient.Close()方法的缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code块,它会影响我的计划的效率。这里的问题是,如果目标主机的存在,一切都OK。但是,如果它确实存在,它需要很长时间才能执行。最后,我发现了udp.Close()占据了大部分的执行时间。如果我不调用close方法,效率还是不错的。

谁能帮我告诉我有什么缺点,如果我不调用close方法?非常感谢你。

  {//这是我的code座,我需要执行它很多很多次。
。字符串ip地址= Dns.GetHostAddresses(乔-PC)FirstOrDefault()的ToString();
UdpClient UDP =新的UdpClient(ip地址,端口);
udp.Send(味精,msg.Length);
udp.Close();
UDP = NULL;
}
 

解决方案

缺点是,你将有一个资源泄漏。您的可以的是足够幸运的垃圾收集情况往往不够,它并没有表现出自己在你的程序,但为什么抓住这个机会?从上 的文档关闭

  

关闭禁用底层的 插槽 ,并释放所有的管理和使用的 UdpClient

请注意,它谈的非托管资源。它要么做它在关闭只有的由 UdpClient 运行某些code发布C $ C> / 处置,或者它做在其的Finalize 方法 - 不出意外会导致他们被释放(假设程序保持运行)。

您的可以的能够通过使用 Task.Run 关闭运行成本$ C>有它在另一个线程上运行 - 但你必须权衡这样做的成本


或者,把它放在更具体 - 你说你需要这个方法来运行很多次。如果不清理你的资源在这里,你会增加成功的机会,一个随后的调用将完全失败,因为它无法的获得的所需资源(他们在现有的非全绑起来 - 关闭 D UdpClient 实例)。


和,在我的评论指出,下面一行是没有意义的:

  UDP = NULL;
 

用于在COM时代的VB使用这样的code,但它在.NET世界中没有立足之地。

I have the following code block, and it affects my program efficiency. The problem here is if the target host exists, everything is OK. But if it does exist, it takes too long to execute. Finally, I found out the "udp.Close()" occupies the most of execution time. If I does not invoke the close method, the efficiency is good.

Can anyone help me tell me what is the drawback if I do not invoke the close method?? Thank you very much.

{ // This is my code block, I need to execute it many many times.
string ipAddress = Dns.GetHostAddresses("joe-pc").FirstOrDefault().ToString();
UdpClient udp = new UdpClient(ipAddress, Port);
udp.Send(msg, msg.Length);
udp.Close();
udp = null;
}

解决方案

The drawback is that you'll have a resource leak. You may be lucky enough that garbage collection happens often enough that it doesn't demonstrate itself in your program, but why take the chance? From the documentation on Close:

The Close disables the underlying Socket and releases all managed and unmanaged resources associated with the UdpClient.

Note, it talks of unmanaged resources. These will only be released by the UdpClient running some code - it either does it in Close/Dispose, or it has to do it in its Finalize method - nothing else will cause them to be released (assuming the program stays running).

You may be able to hide the cost of the Close operation by using Task.Run to have it run on another thread - but you'd have to weigh up the cost of doing so.


Or, to put it in more concrete terms - you say that you need this method to run many times. By not cleaning up your resources here, you would increase the chances that a subsequent call will fail completely because it cannot acquire the required resources (they're all tied up in existing, non-Closed UdpClient instances).


And, as indicated in my comment, the following line is pointless:

udp = null;

Such code used to be of use in COM era VB, but it has no place in the .NET world.

这篇关于什么是如果我不调用UdpClient.Close()方法的缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆