我一直在慢慢地耗尽内存。我使用 WinDbg 查看了内存转储,它看起来像这样:
000007f975ee0630 557377 41027736 System.Object[]
000007f975f004d0 18781 47746604 System.Byte[]
000007f975efc358 561433 54013658 System.String
000000000137b170 7616 1322123700 Free
Total 7627101 objects
Fragmented blocks larger than 0.5 MB:
Addr Size Followed by
000000004a9f62b0 18.2MB 000000004bc28050 System.Threading.OverlappedData
000000004dc2ce68 16.1MB 000000004ec522e8 System.Threading.OverlappedData
0000000050adaec0 10.3MB 0000000051525620 System.Threading.OverlappedData
000000005d47fd98 10.2MB 000000005deab618 System.Threading.OverlappedData
0000000071718ab8 23.0MB 0000000072e13a80 System.Threading.OverlappedData
0000000072e13e50 11.8MB 00000000739e4898 System.ServiceModel.Channels.SocketConnection
00000000801a7830 29.7MB 0000000081f5dd60 System.Threading.OverlappedData
000000008264ab58 14.0MB 000000008344bac0 System.Threading.OverlappedData
000000008344bb30 11.6MB 0000000083fecf80 System.Threading.OverlappedData
0000000083fecff0 13.6MB 0000000084d8dae8 System.Threading.OverlappedData
0000000084d8db58 148.3MB 000000008e1d65f8 System.Threading.OverlappedData
0000000093db04e0 19.4MB 00000000951158b8 System.Threading.OverlappedData
0000000095115928 33.9MB 00000000972f2620 System.Threading.OverlappedData
00000000abaa6738 71.9MB 00000000b0285cb0 System.Byte[]
请注意,碎片堆中有更多项目,我只粘贴了超过 10MB 的项目。您可以看出主要问题是 System.Threading.OverlappedData。
最近我重构了所有代码以使用 async & await 到这样的东西:
await Task.Factory.FromAsync<string, MessageSender>(
this.MessageFactory.BeginCreateMessageSender,
this.MessageFactory.EndCreateMessageSender,
this.Topic.Path,
null)
.ConfigureAwait(false);
有很多套接字入站(socket)和出站(azure service bus)通信。如何避免这样的内存碎片?