WPF WCF MVVM OutOfMemoryException - asp.net

I am working on this WCF-WPF app. I am frequently getting this error on an asynchronous XamDataGrid. Trying to make a WCF call and populate the Grid. I initially thought this is something to do with the large amounts of data returned from WCF and I made the call return data is much small pieces, by calling only for a category only. This error comes up randomly, not always on the same set of data. Reproducible enough in 15-20 tries.
I am running Windows XP (32bit), Dual Core, with 4GB of ram. When this exception is raised, client machine only uses about 2GB of RAM, and on the server W3WP is using only 800MB (of 6GB, 3 cores. Total memory use on server is ~2GB).
This is only happening on XP machines. Does not get this error on Windows 7 box.
Please guide me how to resolve this issue.
Thanks in advance
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.
Below id the Exception details:
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 exceptions can happen for a lot of reasons in general, and in web apps in particular.
First, if you're running on IIS, there are settings specific to IIS to limit memory, see here: http://blogs.msdn.com/b/pfedev/archive/2009/01/22/memory-based-recycling-in-iis-6-0.aspx
Depending on your configuration, pools, etc., these limits may not apply to .NET web applications.
Now, in ASP.NET, there is also a setting memoryLimit in the machine.config file that specifies the maximum allowed memory size, as a percentage of total system memory, that the worker process can consume before ASP.NET launches a new process and reassigns existing requests.
The default is 60%, which is equal to 1.2G on a 2G machine.
However,... whatever you configure, 800M is the practical limit for ASP.NET apps on a 32-bit machine, see a good explanation here: Understanding ASP.Net memory
The solution to avoid this is to change the way your system works and break it in smaller pieces.
That's why by default, WCF has been carefully configured with so many limits (see here for an extensive list: http://weblogs.asp.net/paolopia/archive/2008/03/23/wcf-configuration-default-limits-concurrency-and-scalability.aspx) ... which almost every developer tends to change and max-out as soon as he discovers it :-)

You won't be able to utilise that 4 GB of RAM with a 32 bit OS.
http://chris.pirillo.com/32-bit-windows-and-4gb-of-ram/
If your application is consuming more and more memory as you use it, you may want to look at your memory management. Look at implementing the IDisposable interface for classes with unmanaged resources. Also, avoid keeping references to objects once you've finished using them. Events are particularly bad for this.
How do events cause memory leaks in C# and how do Weak References help mitigate that?
You could also look at streaming to a temporary file instead of streaming to memory.

Feel Relieved to find the answer that Microsoft Screwed it up all... :). that blame almost saved my Job... Here is the story..
WCF has a default message size of 64kb. One cannot find the reason for this on MSDN.. and so when we need to change the max message size to whatever we want. The problem is that the threshold for LOH is 85000b. If the message size is more than that, the object will be placed in LOH, your profiler will show that the objects have collected.. but when you see the LOH size in Sysinternals Process Explorer, Process properties window, you still see that the memory using is increasing. At around 800MB, our mighty Dr Watson comes up and kick of this application. Apparently this is a known issue with WCF & .net 2.0
Issue still exists in .net 4.0 but Microsoft released a quickfix. Sysinternals Process explorer is the tool which helped diagnose this issue. See Screenshot

Related

ASP.NET session state: The timeout period elapsed prior to obtaining a connection from the pool

We are getting this error abruptly in our production environment. Whenever this happens, ASP NET session Db is flooded with the timeout exceptions for a few minutes and then it comes back to normal.
Application configuration: Web form application using ASPState session Db for session management. This session management is on an exclusive DB and we are the only app using this DB. Application has a separate DB for other app schema needs (on the same sql server though).
Complete error stack:
Timestamp: 13/07/2017 04:32:35
Message: Unable to connect to SQL Server session database.
Additional Info: Requested URL[https://www.myAppURL/Services/SessionStateService.asmx/myAction], Browser[Chrome59]
Inner Exception: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
Category: Error&Warning
Priority: -1
EventId: 0
Severity: Error
Title:ERROR
Machine: myServer
Application Domain: /LM/W3SVC/2/ROOT-1-131443848021611848
Process Id: 11532
Process Name: c:\windows\system32\inetsrv\w3wp.exe
Win32 Thread Id: 8236
Thread Name:
Extended Properties: Stack Trace - at System.Web.SessionState.SqlSessionStateStore.ThrowSqlConnectionException(SqlConnection conn, Exception e)
at System.Web.SessionState.SqlSessionStateStore.SqlStateConnection..ctor(SqlPartitionInfo sqlPartitionInfo, TimeSpan retryInterval)
at System.Web.SessionState.SqlSessionStateStore.GetConnection(String id, Boolean& usePooling)
at System.Web.SessionState.SqlSessionStateStore.DoGet(HttpContext context, String id, Boolean getExclusive, Boolean& locked, TimeSpan& lockAge, Object& lockId, SessionStateActions& actionFlags)
at System.Web.SessionState.SqlSessionStateStore.GetItemExclusive(HttpContext context, String id, Boolean& locked, TimeSpan& lockAge, Object& lockId, SessionStateActions& actionFlags)
at System.Web.SessionState.SessionStateModule.GetSessionStateItem()
at System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Transaction Id - 9685d278-61dc-4be9-8139-e0bd4131910c
The database server that stores session state is just an ordinary database, subject to all the performance and concurrency limits normally found in a database. In this case, it sounds like either there is too much load or transactions are being blocked by table or page locks.
You can try a couple things.
Increase the number of connections, e.g. in the connection string add a "Max Pool Size" attribute and set it higher than the current value. The default is 100.
Increase the pace at which stale sessions are cleaned up. Open up the SQL database and find the SQL agent job "delete expired sessions." I believe the default frequency is one minute; try setting it to 30 seconds. If the job cleans up too many sessions at once, it can promote row locks to page or table locks, which will block all other connections until the delete operation is done. Smaller bites are better.
Reduce the number of pages that require session state, e.g. set this in web forms:
<# Page EnableSessionState="false">
In MVC:
[SessionState(SessionStateBehavior.Disabled)]
If you can do this on 50% of your pages, you will reduce the SQL load by about 50%.
If you do not disable session state, even if you don't have any code that uses it in that page, the framework still does the overhead of loading and serializing the session state data at the beginning and end of the pipeline. So you may as well disable it if you're not using it.
Make sure you are cleaning up your session variables when you're done with them (e.g. with Session.Remove). If you never clean them up, they just stick around forever, sucking up bandwidth and database I/O. Remember, with out-of-proc session state, 100% of your session state variables get dragged over the wire every single time, whether you are using them or not.
If none of the above works, try try reading this article and implementing the solution in there. Basically it's a hack that replaces a Microsoft stored procedure with one that uses a cursor in order to avoid page or table locks. I wouldn't do this except as a last resort.

What could cause GDI+ Initialize exception

I spend a lot of time to search answer but I didn't found it. We have console application (exe) with C1 Report Component for automated printing task. This application is executed from cmd file and cmd file is called by cmdexec from SQL Server 2014 every minute. It's work very fine but there is exceptiton GDI+ Initialize - System.Drawing.SafeNativeMethods+Gdip.Initialize aproximetelly montly. There is no chance to solve this so we must restart server.
I tried to create cmd file with echo command. It was worked fine and I got output from echo. So I created application with Console.Writeline("Hello world") only. It was worked fine. Then I add reference to System.Drawing. Nothing happend - ouput was produced. Then I created System.Drawing.Bitmap object and I got GDI+ Initialize exception.
Job on SQL server run as SQLRUNSERVICES and it is in Administrator group. But when I log as SQLRUNSERVICES and launch our application in command line, application works fine without exception. When I log as another user there is no problem with this. Only with SQLRUNSERVICES throught SQL Proxy.
I suppose there must be something wrong with windows session. This application starts, prints some reports and finishes. So I suppose when application finishes memory will be free and GDI object will be free so. I check if we have call dispose method for each object working with GDI.
Can I have some chance to see GDI objects used by session? Or what could cause this exception? I cannot use task manager because when application is not running there is nothing to see.
Thank you for replies. It is very stresfull problem for us because it is going repeatedly every month after restart server.
We created testing application to print most quickly and we got this exception for one day. We call Dispose method for C1Report ofcourse. We will try to use using section. But it looks like C1 error. Test application printed about 65000 count of document and then crashed by GDI+ Initialization Exception. There is stack:
System.TypeInitializationException: The type initializer for 'Gdip' threw an exception. ---> System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
at System.Drawing.SafeNativeMethods.Gdip.Initialize()
at System.Drawing.SafeNativeMethods.Gdip..cctor()
--- End of inner exception stack trace ---
at System.Drawing.SafeNativeMethods.Gdip.GdipCreateBitmapFromScan0(Int32 width, Int32 height, Int32 stride, Int32 format, HandleRef scan0, IntPtr& bitmap)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height, PixelFormat format)
at System.Drawing.Bitmap..ctor(Int32 width, Int32 height)
at C1.C1Preview.GraphicsHolder.FromBitmap()
at C1.C1Preview.GraphicsHolder.FromScreen()
at C1.C1Preview.C1PrintDocument.SelectBestMeasurementDevice(Boolean showWarning, MeasurementDeviceEnum& measurementDevice, String& measurementPrinterName, GraphicsHolder& measurementGraphicsHolder)
at C1.C1Preview.C1PrintDocument.#fFe()
at C1.C1Preview.C1PrintDocument.SetCreationDevice(MeasurementDeviceEnum creationDevice, String creationPrinterName)
at C1.C1Report.Layout..ctor(C1Report report)
at C1.C1Report.C1Report.Initialize()
at C1.C1Report.C1Report..ctor()

NReco.PdfGenerator in ASP.NET stopped working on server

Local PDF generation works well, but the same application stoped working on server. Event log says:
Faulting application wkhtmltopdf.exe, version 0.12.2.1, time stamp 0x54bd0265, faulting module MSVCP120.dll, version 6.0.6002.18881, time stamp 0x51da3e00, exception code 0xc0000135, fault offset 0x0006f52f, process id 0x44b8, application start time 0x01d07ce4c087128f.
and
Exception information:
Exception type: IOException
Exception message: The pipe has been ended.
at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath)
at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(String htmlFilePath, Byte[] inputBytes, String coverHtml, String outputPdfFilePath, Stream outputStream)
I've spent 4 hours wrestling with this today, so I thought i'd post my solution to help anyone else suffering in thr future.
wkhtmltopdf was updated and worked on my local machine.
Pushed the code to the server and nothing.
I eventually tracked this down to needing to install the 32 bit version of C++Runtime from here https://www.microsoft.com/en-GB/download/details.aspx?id=40784
In my case, due to some 3rd party libraries I cannot remove yet, I was forced to run the website in 32 bit mode. As my server is 64 bit, the 32 bit versions were not present. (As my PC is a Dev Machine, I assume that I got all the relevent libraries from MS, hence why I had no local issue.)
Solution is to check accesibility of dll's msvcp120.dll and msvcr120.dll. Local copy of libraries into app bin dir solve my problem.

Why do I get ErrorCode <ERRCA0022> when I take down one velocity cache host?

I'm getting the following exception in my web app when I take down one node of a three node cluster which is hosting my users’ sessions.
The session cache also has secondaries on with no eviction.
Here is the error message and stack:
Exception information:
Exception type: DataCacheException
Exception message: ErrorCode<ERRCA0022>:Cache::GetAndLock: There is a temporary failure, please retry after some time.
Stack trace: at Microsoft.Data.Caching.DataCache.ExecuteAPI(RequestBody reqMsg, String apiName)
at Microsoft.Data.Caching.DataCache.InternalGetAndLock(String key, TimeSpan timeout, DataCacheLockHandle& lockHandle, String region)
at Microsoft.Data.Caching.DataCache.GetAndLock(String key, TimeSpan timeout, DataCacheLockHandle& lockHandle)
at Microsoft.Data.Caching.DataCacheSessionStoreProvider.GetItemExclusive(HttpContext context, String id, Boolean& locked, TimeSpan& lockAge, Object& lockId, SessionStateActions& actions)
at System.Web.SessionState.SessionStateModule.GetSessionStateItem()
at System.Web.SessionState.SessionStateModule.PollLockedSessionCallback(Object state)
If I retry the request a few moments later the message will go away. Why am I getting this message in the first place and shouldn’t the server figure it out on its own that a host is down and switch to one of the other clients. I've defined the session client as simple in my web.config file just in case it was a routing problem but the issue still arises.
please refer to the following blogs:
http://blogs.msdn.com/velocity/archive/2009/04/30/the-dreaded-error-cache-get-the-request-timed-out.aspx
http://blogs.msdn.com/velocity/archive/2009/06/17/troubleshooting-velocity-series.aspx
To answer your question about the server figuring out this on its own; it can actually do that, but since we cannot make a choice from the application developer's perspective (to retry or failfast), we leave it tothe application to employ the right behaviour for itself (retry, failing back to a DB, etc.). Hence we by default retry but we also having an aye on having some retry logic inbuilt if some application wants it. Hope this answers your query.

System.Web.HttpUnhandledException

I got error in my aspx.Below the information of the error.
Error Message : Exception of type 'System.OutOfMemoryException' was thrown.
Source : mscorlib
StackTrace : at System.String.GetStringForStringBuilder(String value, Int32 startIndex, Int32 length, Int32 capacity)
at System.Text.StringBuilder.set_Capacity(Int32 value)
at System.Xml.BufferBuilder.ToString()
at System.Xml.XmlTextReaderImpl.ParseText()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlReader.ReadElementString()
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderService.Read3_GetXmlResponse()
at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer9.Deserialize(XmlSerializationReader reader)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
-----
-----
-----
Target Site : System.String GetStringForStringBuilder(System.String, Int32, Int32, Int32)
Can any one help me ?
Thx
Don't know whether this solves your issue or not. But please take a look at this one
Problems with System.OutOfMemoryException At System.String.GetStringForStringBuilder in 32-Bit Managed Solutions
When you want to add text to StringBuilder, and the current capacity is not enough StringBuilder will try to double it's capacity.
So max capacity for string is 2^32 but in your case it will want to go from 17MB to 34MB. may be you are trying to append the text to a big StringBuilder?
You need to use .NET memory profiler to check where are memory leaks in your application.
http://www.google.com/search?ie=UTF-8&oe=UTF-8&sourceid=navclient&gfns=1&q=.NET+memory+profilers
It looks like you are operating with very big string there, or some other memory leaks caused memory pressure.
The error occurs when access web service that return xml.
the return will be used in datatable.
Your server are running out of memory. There's really three possibilities here:
You need a new server. Probably not the cause.
Your server is misconfigured. A possibility, especially given the stack trace.
You are doing something drastically wrong. Probably the cause.
Beyond this, noone will be able to even hazard a guess at what is wrong without actually seeing the code around where the error is happening. Though, if that's a complete stack trace it may be the case that #2 is coming into play. It may be that #1 is coming into play if your server is being swamped with traffic, but really, it would take alot of traffic on any semi-modern server to trigger an out of memory error.

Resources