asp.net - WPF WCF MVVM 内存不足异常

标签 asp.net wpf wcf mvvm out-of-memory

我正在开发这个 WCF-WPF 应用程序。我经常在异步 XamDataGrid 上收到此错误。尝试进行 WCF 调用并填充网格。我最初认为这与从 WCF 返回的大量数据有关,并且我通过仅调用一个类别来进行调用,返回的数据非常小。此错误是随机出现的,并不总是出现在同一组数据上。 15-20 次尝试即可重现。

我运行的是 Windows XP(32 位)、双核、4GB 内存。当引发此异常时,客户端计算机仅使用大约 2GB 的 RAM,而服务器上的 W3WP 仅使用 800MB(6GB,3 个核心。服务器上的总内存使用量约为 2GB)。

这只发生在 XP 机器上。在 Windows 7 机器上不会出现此错误。

请指导我如何解决此问题。

提前致谢

Event Viewer Logs this Message:
Event Type: Error
Event Source:   .NET Runtime 2.0 Error Reporting
Event Category: None
Event ID:   5000
Date:       10/13/2010
Time:       10:50:07 AM
User:       N/A
Computer:   COMP-DC7800
Description:
EventType clr20r3, P1 appname.exe, P2 2.0.0.21872, P3 4cb0a1b1, P4 mscorlib, P5 2.0.0.0, P6 492b834a, P7 35df, P8 45, P9 system.outofmemoryexception, P10 NIL.

下面是异常详细信息:

System.OutOfMemoryException was unhandled
Message="Exception of type 'System.OutOfMemoryException' was thrown."
Source="mscorlib"
StackTrace:
   at System.IO.MemoryStream.set_Capacity(Int32 value)
   at System.IO.MemoryStream.EnsureCapacity(Int32 value)
   at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.Xml.XmlMtomReader.MimePart.GetBuffer(Int32 maxBuffer, Int32& remaining)
   at System.Xml.XmlMtomReader.Initialize(Stream stream, String contentType, XmlDictionaryReaderQuotas quotas, Int32 maxBufferSize)
   at System.Xml.XmlMtomReader.SetInput(Stream stream, Encoding[] encodings, String contentType, XmlDictionaryReaderQuotas quotas, Int32 maxBufferSize, OnXmlDictionaryReaderClose onClose)
   at System.Xml.XmlMtomReader.SetInput(Byte[] buffer, Int32 offset, Int32 count, Encoding[] encodings, String contentType, XmlDictionaryReaderQuotas quotas, Int32 maxBufferSize, OnXmlDictionaryReaderClose onClose)
   at System.ServiceModel.Channels.MtomMessageEncoder.MtomBufferedMessageData.TakeXmlReader()
   at System.ServiceModel.Channels.BufferedMessageData.DoTakeXmlReader()
   at System.ServiceModel.Channels.BufferedMessageData.GetMessageReader()
   at System.ServiceModel.Channels.MessageHeaders.GetBufferedMessageHeaderReaderAtHeaderContents(IBufferedMessageData bufferedMessageData)
   at System.ServiceModel.Channels.MessageHeaders.GetBufferedMessageHeaderReader(IBufferedMessageData bufferedMessageData, Int32 bufferedMessageHeaderIndex)
   at System.ServiceModel.Channels.MessageHeaders.GetReaderAtHeader(Int32 headerIndex)
   at System.ServiceModel.Channels.WsrmMessageInfo.Get(MessageVersion messageVersion, ReliableMessagingVersion reliableMessagingVersion, IChannel channel, ISession session, Message message, Boolean csrOnly)
   at System.ServiceModel.Channels.ReliableDuplexSessionChannel.HandleReceiveComplete(IAsyncResult result)
   at System.ServiceModel.Channels.ReliableDuplexSessionChannel.OnReceiveCompletedStatic(IAsyncResult result)
   at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
   at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously)
   at System.ServiceModel.Channels.ReliableChannelBinder`1.InputAsyncResult`1.OnInputComplete(IAsyncResult result)
   at System.ServiceModel.Channels.ReliableChannelBinder`1.InputAsyncResult`1.OnInputCompleteStatic(IAsyncResult result)
   at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
   at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously)
   at System.ServiceModel.Channels.InputQueue`1.AsyncQueueReader.Set(Item item)
   at System.ServiceModel.Channels.InputQueue`1.Dispatch()
   at System.ServiceModel.Channels.InputQueue`1.OnDispatchCallback(Object state)
   at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2()
   at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.OnSecurityContextCallback(Object o)
   at System.Security.SecurityContext.Run(SecurityContext securityContext, ContextCallback callback, Object state)
   at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke()
   at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks()
   at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(Object state)
   at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
   at System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
   at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)

最佳答案

一般来说,发生 OutOfMemory 异常的原因有很多,尤其是在网络应用中。

首先,如果您在 IIS 上运行,则存在特定于 IIS 的设置来限制内存,请参阅此处:http://blogs.msdn.com/b/pfedev/archive/2009/01/22/memory-based-recycling-in-iis-6-0.aspx

根据您的配置、池等,这些限制可能不适用于 .NET Web 应用程序。

现在,在 ASP.NET 中,ma​​chine.config 文件中还有一个设置 memoryLimit,用于指定允许的最大内存大小,以系统总内存的百分比表示工作进程在 ASP.NET 启动新进程并重新分配现有请求之前可以消耗的内存。

默认为 60%,相当于 2G 机器上的 1.2G。

但是,...无论您配置什么,800M 都是 32 位计算机上 ASP.NET 应用程序的实际限制,请参阅此处的详细解释:Understanding ASP.Net memory

避免这种情况的解决方案是改变系统的工作方式并将其分解为更小的部分。

这就是为什么默认情况下,WCF 被仔细配置了如此多的限制(请参阅此处查看详细列表: http://weblogs.asp.net/paolopia/archive/2008/03/23/wcf-configuration-default-limits-concurrency-and-scalability.aspx )...几乎每个开发人员一发现它就倾向于更改并最大化: -)

关于asp.net - WPF WCF MVVM 内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3916642/

相关文章:

c# - 为什么我不能使用HttpClient 登录这个ASP.NET 网站?

c# - XML 模式不适用于 Web 服务软件工厂

c# - DateTimeOffset 的 WCF 服务引用?不使用 FCL 类型

c# - WCF 服务调用总是在 30 秒后失败,并显示 (502) Bad Gateway

javascript - 使用按钮 OnClick 事件调用 Javascript 函数

c# - 没有使用 Application_Error 记录错误

c# - 为什么我的 wcf 应用程序不能使用和保存 session ?

wpf - 调整托管数百个类似控件的 WPF 应用程序性能

c# - WPF - TreeView 隐藏展开图标(箭头)

c# - 使 AvalonEdit MVVM 兼容