我经常慢慢跑出记忆。我已经使用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& amp;的代码。等待这样的事情:
await Task.Factory.FromAsync<string, MessageSender>(
this.MessageFactory.BeginCreateMessageSender,
this.MessageFactory.EndCreateMessageSender,
this.Topic.Path,
null)
.ConfigureAwait(false);
有很多套接字入站(套接字)和出站(天蓝色服务总线)通信。如何避免这种内存碎片?
答案 0 :(得分:0)
我认为你应该检查那些地方:
我现在正在处理类似的问题:我的服务器在异步套接字上收到大量客户端请求,但由于种种原因,客户端无法正常结束通信(很可能是由于频繁的IP更改 - 他们在GPRS上移动/ 3G)。这似乎使回调永远等待,并且套接字上的EndReceive永远不会被调用,这使得那些重叠对象(AsyncPinned)在内存中永远悬挂,导致高内存使用和碎片。因此,关于限制并发连接数量并自己确保过期连接被手动关闭的想法。