问题
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;
}
回答1:
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-Close
d 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.
回答2:
The Close()
disables the underlying Socket and releases all managed and unmanaged resources associated with the UdpClient.
if u not closed then it doesn't disables & deallocate resources like ur port & ipaddress
回答3:
Instead of Using Send
could you use BeginSend
and then in your callback handle an exception when you attempt to Close if that is actually the problem?
回答4:
You're targeting joe-pc presumably with different ip addresses as the dns record changes, but you reuse the same UdpClient for each send. Just remember to Close() it when you're all done.
Use
//
// Summary:
// Sends a UDP datagram to a specified port on a specified remote host.
//
// Parameters:
// dgram:
// An array of type System.Byte that specifies the UDP datagram that you intend
// to send represented as an array of bytes.
//
// bytes:
// The number of bytes in the datagram.
//
// hostname:
// The name of the remote host to which you intend to send the datagram.
//
// port:
// The remote port number with which you intend to communicate.
//
// Returns:
// The number of bytes sent.
//
public int Send(byte[] dgram, int bytes, string hostname, int port);
and skip the dns lookup too.
回答5:
I know this is a while but I stumbled today upon this question and wanted to add:
udp=null;
is not pointless imho. You can see in some cases in a memory profiler that your instance is still available if you dont set it to null.
来源:https://stackoverflow.com/questions/12776563/what-is-the-drawback-if-i-do-not-invoke-the-udpclient-close-method