什么可以“Pin"”Silverlight中的内存对象

本文关键字:Silverlight 对象 内存 quot Pin 什么 | 更新日期: 2023-09-27 18:04:46

我正在调试一个应用程序,它似乎像疯了一样泄漏内存;大部分似乎是由于固定对象的碎片(在WriteableBitmap中下载的图像数据)。然而,我并不是有意使用GC。把手之类的东西。我所做的就是将数据存储在MemoryStream中,并像这样引用它。

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

EDIT:对于每个请求,这里是系统上一个GCRoot的输出(稍微清理过)。Int32数组与大块空闲内存相邻。这是所有大型空闲块的代表。

EDIT 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[])

什么可以“Pin"”Silverlight中的内存对象

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

我们将大的队列对象分解成一组较小的队列,以便在它们缩小到正常运行大小时收集数据结构。在此代码更改之前,一旦队列增长超过阈值,内存将永远不会被释放回操作系统。

这是你正在经历的吗?

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

您还应该检查是否有任何处理程序或异步操作,如WCF客户端或Http Web服务客户端打开状态,在离开控件或视图模型或模型之前。如果在退出控件、视图模型或模型时固定了任何对象,则这些对象将产生内存泄漏。

还请记住,如果这些对象最终超过85000字节,它们最终将在第2代中,直到应用程序(域)终止才进行垃圾收集。

如果对象小于85K,并且在第0代GC中幸存。它被提升到第1代,当存活或未被释放或其他东西持有时,它将结束其生命到第2代,在SL中不被收集。

还需要注意:如果频繁地释放/分配这些对象将导致内存碎片化。如果你的程序需要在内存中有连续的空间,并且没有那么大的空闲块,因为碎片,你会得到内存溢出异常。