Unnecesary use of GCHandle in Windows Socket code #86513
Labels
area-System.Net.Sockets
bug
in-pr
There is an active PR which will close this issue when it is merged
os-windows
tenet-performance
Performance related issue
Milestone
This was reported by customer that application can crash with OoMs because memory can become highly fragmented because of small chunks of pinned memory. That generally points to socketAddress in
ConnectEx
. (used bySocket.ConnectAsync
)At the moment we hold the
GCHandle
even after the Connect is finished through life of the Socket. That can be fixed easily but furthermore it does not seems to be needed at all.For ConnextEx and WSASendTo the socket address is documented as
[In]
and needs to be preserved only through initial call and not through life of the overlapped IO. (confirmed with Windows networking team) We can use simplefixed
instead of allocating GCHandle and pinning the memory in heavy way.The WSAReceiveFrom is different as the SocketAddress is
out
and sockerAddressLength is[in,out]
. We may use NativeMemory for that as the current logic with caching GCHanlde may not work anyway. TheReceiveFrom
will likely receive packets from various sources (for simple duplex simpleReceive
can be used) and we will keep allocating GCHandle for (almost) every packet.While the first part should be easy to fix (and TCP gets most use) solving the UDP receiving may take some more effort. That would also contribute to #30797.
The text was updated successfully, but these errors were encountered: