使用System.Threading.OverlappedData (async await)会导致内存碎片



我一直在慢慢耗尽内存。我已经使用了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 Task.Factory.FromAsync<string, MessageSender>(
                                  this.MessageFactory.BeginCreateMessageSender, 
                                  this.MessageFactory.EndCreateMessageSender, 
                                  this.Topic.Path,
                                  null)
      .ConfigureAwait(false);

有很多套接字入站(socket)和出站(azure服务总线)通信。如何避免这种内存碎片?

我认为你应该检查这些地方:

  • KB947862

  • this SO问题与讨论

我现在正在处理一个类似的问题:我的服务器在异步套接字上接收许多客户端请求,但出于多种原因,客户端没有正确结束他们的通信(很可能是由于频繁的IP更改-他们在GPRS/3G上移动)。这似乎使回调永远等待和套接字上的EndReceive永远不会被调用,这使得那些重叠的对象(这是asyncpin)永远在内存中摆动,导致高内存使用和碎片化。因此,限制并发连接的数量,并确保您自己手动关闭过期的连接。

相关内容

  • 没有找到相关文章

最新更新