问题
I have a C#/WPF application that is meant to run on a small panel PC with 512 MB of memory. If it is left to run, an OutOfMemoryException is eventually thrown. This can take 12+ hours up to 2 days to happen.
Using both ProcessExplorer and .NET Memory Profiler, I see no evidence that the memory used by my application is growing over time. There is just a typical pattern of a little growth followed by garbage collection.
I've added code to print out all running processes and their memory information when the exception is thrown. I'm currently just waiting around for it to happen again. I should mention that PCs are flash based and paging is disabled. The 512 is a hard limit.
It has happened on two separate PCs. The exception information was the same in both cases:
Top Level Exception - System.OutOfMemoryException: Insufficient memory to continue the execution of the program.
at System.Windows.Media.Composition.DUCE+Channel.SendCommand(Byte* pCommandData, Int32 cSize)
at System.Windows.Media.MediaContext.EnterInterlockedPresentation()
at System.Windows.Media.MediaContext.ScheduleNextRenderOp(TimeSpan minimumDelay)
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.AnimatedRenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
Any insight is appreciated!
回答1:
Since you are a 'small panel PC', maybe you are calling some non-'.Net' code to control some hardware. Is so, maybe that part of the program might be using a fair amout of memory (but still not leaking any). To get the garbage collector to work harder in that case, look into adding a GC.AddMemoryPressure() call.
回答2:
It turns out that a variety of issues were causing my application to crash. I've detailed them in another post here.
回答3:
I came across a very good article that examines the error in depth.
WPF Render Thread Errors
The most telling paragaph: don't bother trying to analyze the call stack you're given:
Due to the design, unfortunately, the exception and callstack you see on the UI thread aren't typically helpful in diagnosing the true cause of the problem. This is because by the time the exception is thrown, it is already after the point of failure on the render thread. By this time, critical state has been lost on the render thread that would help us understand where and why the failure occurred.
来源:https://stackoverflow.com/questions/5029885/c-sharp-outofmemoryexception-how-to-track-down-the-culprit