什么可以在Silverlight中“固定”内存中的对象?

时间:2011-08-08 23:34:53

标签: c# silverlight clr windbg

我正在调试一个似乎像疯了一样泄漏内存的应用程序;大多数情况似乎是由于固定对象的碎片(在WriteableBitmap中下载的图像数据)。但是,我并不是故意使用GC.Handle或类似的东西。我所做的只是将数据存储在MemoryStream中,并像这样引用它。

什么操作将数据固定在内存中,哪些没有明确说明?另外,我怎样才能找到使用WinDbg固定它的内容?

编辑:每个请求,这是一个(稍微清理过)的System.Int32阵列上的一个!GCRoot输出,与一大块可用内存相邻。这是所有大型免费区块的代表。

编辑2:在我的新朋友WinDbg和SOS上花了一些时间后,我发现WriteableBitmapsMemoryStream个对象都被'固定',应该被分配小心防止内存碎片。阅读接受的答案中的文章,以解释为什么需要这样做。

DOMAIN(1AC72358):HANDLE(Pinned):72c12f8:Root:  174c5e20(System.Object[])->
  16533060(Project.ProjectParts.PartContainer)->
  167fe554(Project.ProjectParts.Part.PartActivity)->
  167d21d8(Project.ProjectParts.Sprites.Graphic)->
  16770f28(System.Windows.Controls.Canvas)->
  16770e1c(System.Windows.Controls.Canvas)->
  16770ee4(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  1680e778(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  16770f9c(System.Windows.Controls.Canvas)->
  16819114(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  16819160(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  16818df4(System.Windows.Controls.Canvas)->
  16818e58(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  16819f10(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  168194c4(System.Windows.Controls.Canvas)->
  16819528(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  16819574(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  16819370(System.Windows.Controls.Image)->
  21c82138(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  21c82184(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  168195dc(System.Windows.Media.Imaging.WriteableBitmap)->
  21c7ce2c(System.Int32[])
DOMAIN(1AC72358):HANDLE(AsyncPinned):72c1dfc:Root:  166bae48(System.Threading.OverlappedData)->
  1654d448(System.Threading.IOCompletionCallback)->
  1654c29c(System.Net.Sockets.SocketAsyncEventArgs)->
  1654bad4(System.Net.Sockets.Socket+StaticConnectAsyncState)->
  1654ba40(System.Net.Sockets.SocketAsyncEventArgs)->
  1654b684(System.ServiceModel.Channels.SocketConnectionInitiator+ConnectAsyncResult)->
  1654b414(System.ServiceModel.Channels.ConnectionPoolHelper+EstablishConnectionAsyncResult)->
  1654b3b0(System.ServiceModel.Channels.ClientFramingDuplexSessionChannel+OpenAsyncResult)->
  1654b380(System.ServiceModel.Channels.CommunicationObject+OpenAsyncResult)->
  1654b330(System.ServiceModel.Channels.CommunicationObject+OpenAsyncResult)->
  1654b0f4(System.ServiceModel.Channels.ServiceChannel+SendAsyncResult)->
  1654b070(System.ServiceModel.ClientBase`1+AsyncOperationContext[[Cassandra.Common.WCF.IAsyncWcfRequestProcessor, Cassandra.Common.Silverlight]])->
  1654b05c(System.ComponentModel.AsyncOperation)->
  1654b04c(Project.Common.IoC.InvokeAsyncCompletedEventRequestsArgs)->
  1654afec(System.Action`1[[Project.Common.IoC.ProcessRequestsAsyncCompletedArgsEx, Project.Common.SL]])->
  1654afc8(Project.Common.IoC.AsyncRequestDispatcherEx+<>c__DisplayClass1)->
  1654afa0(Project.Common.IoC.NetResponseReceiver)->
  1653408c(System.Action`2[[Cassandra.Common.ExceptionInfo, Cassandra.Common.Silverlight],[Cassandra.Common.ExceptionType, Cassandra.Common.Silverlight]])->
  16533ffc(Project.ProjectParts.ILE.Services.EngineProxyService+<>c__DisplayClass5)->
  16533fdc(System.Action`1[[Cassandra.Common.ReceivedResponses, Cassandra.Common.Silverlight]])->
  16533fbc(Project.ProjectParts.ILE.Services.IEngineProxyExtensions+<>c__DisplayClass1`2[[Project.Services.RequestsAndResponses.ListMediaServersByTokenRequest, Project.Services.RequestsAndResponses.Silverlight],[Project.Services.RequestsAndResponses.ListInstitutionMediaServersResponse, Project.Services.RequestsAndResponses.Silverlight]])->
  16533f9c(System.Action`1[[Project.Services.RequestsAndResponses.ListInstitutionMediaServersResponse, Project.Services.RequestsAndResponses.Silverlight]])->
  1650a2a0(Project.ProjectParts.ILE.MainPage)->
  1674ea0c(Project.ProjectParts.ActivityTimer)->
  165330a4(Project.ProjectParts.PauseManager)->
  165330bc(System.Collections.Generic.List`1[[Project.ProjectParts.IPausable, ActivityFramework]])->
  166a8610(System.Object[])->
  167ca858(Project.ProjectParts.ActivityTimer)->
  167ca838(Project.ProjectParts.ActivityTimerEventHandler)->
  16533060(Project.ProjectParts.PartContainer)->
  167fe554(Project.ProjectParts.Part.PartActivity)->
  167d21d8(Project.ProjectParts.Sprites.Graphic)->
  16770f28(System.Windows.Controls.Canvas)->
  16770e1c(System.Windows.Controls.Canvas)->
  16770ee4(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  1680e778(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  16770f9c(System.Windows.Controls.Canvas)->
  16819114(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  16819160(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  16818df4(System.Windows.Controls.Canvas)->
  16818e58(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  16819f10(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  168194c4(System.Windows.Controls.Canvas)->
  16819528(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  16819574(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  16819370(System.Windows.Controls.Image)->
  21c82138(System.Collections.Generic.Dictionary`2[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]])->
  21c82184(System.Collections.Generic.Dictionary`2+Entry[[MS.Internal.IManagedPeerBase, System.Windows],[System.Object, mscorlib]][])->
  168195dc(System.Windows.Media.Imaging.WriteableBitmap)->
  21c7ce2c(System.Int32[])
DOMAIN(1AC72358):HANDLE(Pinned):72c2b18:Root:  21c7ce2c(System.Int32[])

2 个答案:

答案 0 :(得分:1)

假设silverlight的工作方式与完整的.net框架相同,那么超过85k的对象几乎不会被垃圾回收。 http://msdn.microsoft.com/en-us/magazine/cc534993.aspx

我们将大型队列对象分解为一个较小队列的数组,然后在数据结构缩小到正常运行大小后允许收集数据结构。在此代码更改之前,一旦队列超出阈值,内存将永远不会释放回操作系统。

这可能是您遇到的情况吗?

答案 1 :(得分:0)

Pin是在操作正在进行或存在处理程序引用时将控件中的Async或处理程序保持为“不在内存中重定位”的机制。

在离开控件或视图模型或模型之前,您还应检查是否有任何处理程序或Async操作(如WCF客户端或Http Web Service客户端打开状态)。如果在退出控件,视图模型或模型时固定了任何对象,则会出现内存泄漏。

还要记住,如果这些对象最终超过85.000bytes,它们最终将在第2代中,这不是Garbage Collected,直到应用程序(域)终止。

如果物体小于85K并且在第0代的GC中存活。它被提升为第1代,当它存活或未被释放或某物持有它将终止它的生命到第2代,在SL中没有收集

另请注意:如果频繁释放/分配这些对象将导致内存碎片化。如果你的程序需要在内存中有连续的空间,并且由于碎片而没有大的空闲块,那么你将失去内存异常。

希望以上有所帮助。