CefSharp icon indicating copy to clipboard operation
CefSharp copied to clipboard

WPF - OutOfMemoryException if app runs for a while

Open SSiefert opened this issue 5 years ago • 6 comments

Bug Report

What version of the product are you using? Lastest two versions: 89.0.170 as well as 88.2.90 Via Nuget.

What architecture x86 or x64? x64

On what operating system? Win10

Are you using WinForms, WPF or OffScreen? WPF (.NET Framework 4.8 as well as .NET Core 3.1 - both tested)

What steps will reproduce the problem? We have an application that is changing the control-tree in wpf in different intervalls. It disposes and creates controls. One part is that we need a browser-control and using for this CefSharp. After a time the applications crashs with a OutOfMemory-exception.

I reproduced the error while using the MinimalExample (WPF). Here's the fork: https://github.com/SSiefert/CefSharp.MinimalExample

The time it requires until the error occurrs differs between a few minutes up to two or three hours. In some cases the memory allocation increases heavily (can be seen in taskmanager).

What is the expected output? What do you see instead? Running stable with no crash and regarding to memory consumption.

Please provide any additional information below. No 3rd party controls.

A screenshot of the OutOfMemory-exception is in the ErrorDetails-folder of the forked MinimalExample: https://github.com/SSiefert/CefSharp.MinimalExample/ErrorDetails

The debug.log of the forked MinimalExample has no errors.

The debug-log of our application has sometimes gpu-crashes: [0409/143351.015:WARNING:angle_platform_impl.cc(48)] Debug.cpp:180 (insertMessage): GL error: HIGH: Internal D3D11 error: HRESULT: 0x887A0005: Error allocating Texture2D [0409/143351.016:ERROR:shared_context_state.cc(797)] SharedContextState context lost via ARB/EXT_robustness. Reset status = GL_UNKNOWN_CONTEXT_RESET_KHR [0409/143351.017:ERROR:gpu_service_impl.cc(1177)] Exiting GPU process because some drivers can't recover from errors. GPU process will restart shortly. [0409/143351.085:WARNING:gpu_process_host.cc(1282)] The GPU process has crashed 1 time(s) [0409/143351.539:WARNING:gpu_process_host.cc(1002)] Reinitialized the GPU process after a crash. The reported initialization time was 261 ms

I also checked the WPF-based error a few years and tried the invalidateDirtyRect-flag. But it has no effect. ( https://github.com/cefsharp/CefSharp/issues/2035 )

Does this problem also occur in the CEF Sample Application from http://opensource.spotify.com/cefbuilds/index.html?

The url http://opensource.spotify.com/cefbuilds/index.html returns a 404.

SSiefert avatar Apr 12 '21 06:04 SSiefert

I reproduced the error while using the MinimalExample (WPF). Here's the fork: https://github.com/SSiefert/CefSharp.MinimalExample

Is there a reason you changed to targeting x86 in commit https://github.com/SSiefert/CefSharp.MinimalExample/commit/7ae26f208ad49494850473002792a7a382636f5d

What's the purpose of the new solution you added?

The time it requires until the error occurrs differs between a few minutes up to two or three hours.

Have you run a memory profiler and confirmed the leak is within CefSharp directly? If the problem is within CEF you'll need to raise an issue at https://bitbucket.org/chromiumembedded/cef/

I'd suggest testing with GPU compositing disabled.

settings.CefCommandLineArgs.Add("disable-gpu-compositing");

A screenshot of the OutOfMemory-exception is in the ErrorDetails-folder of the forked MinimalExample: https://github.com/SSiefert/CefSharp.MinimalExample/ErrorDetails

Link is broken. What is the stack trace of the exception? A text version rather than a screenshot please.

I also checked the WPF-based error a few years and tried the invalidateDirtyRect-flag. But it has no effect.

What exactly did you try?

The url http://opensource.spotify.com/cefbuilds/index.html returns a 404.

It looks like somehow you've used a very old version of the bug report temple. Please test with the CEF Sample application, the details are in the bug report temple at https://github.com/cefsharp/CefSharp/blob/master/.github/ISSUE_TEMPLATE/bug_report.md#bug-report

amaitland avatar Apr 12 '21 06:04 amaitland

Is there a reason you changed to targeting x86 in commit SSiefert/CefSharp.MinimalExample@7ae26f2

It seems that x86 leads faster to the error than x64. But it occurs in both cases.

What's the purpose of the new solution you added?

A solution having the WPF-project was missing. Without a solution compiling was not possible.

I also checked the WPF-based error a few years and tried the invalidateDirtyRect-flag. But it has no effect. What exactly did you try?

I set the RenderHandler as described by you in this post: https://github.com/cefsharp/CefSharp/issues/2035#issuecomment-555296505

chromium.RenderHandler = new WritableBitmapRenderHandler(dpiX, dpiY, true);

Link is broken. What is the stack trace of the exception? A text version rather than a screenshot please.

Sorry for the broken lin. That's the one: https://github.com/SSiefert/CefSharp.MinimalExample/blob/master/ErrorDetails/cefsharp%20outofmemoryexception.png Getting the exception the next time I paste the text.

Have you run a memory profiler and confirmed the leak is within CefSharp directly?

I did not yet use a memoryprofiler.

I'm not completely sure where the error is located. We also tried WebView2 of Microsoft that also uses chromium. Using this the error does not occur. But this is just an indicate, I know.

Up to now it just a feeling that it could be directly in CefSharp or WPF since it is similar to https://github.com/cefsharp/CefSharp/issues/2035 and WebView2 does not have this issue.

I'm running now the MinimalExample with memoryprofiler and disabled gpu compositing.

SSiefert avatar Apr 12 '21 12:04 SSiefert

Exception 1

This is exception stacktrace of OutOfMemoryException

Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst:
System.Windows.Media.Imaging.WriteableBitmap.WriteableBitmap(int, int, double, double, System.Windows.Media.PixelFormat, System.Windows.Media.Imaging.BitmapPalette)
CefSharp.Wpf.Rendering.WritableBitmapRenderHandler.CreateOrUpdateBitmap.AnonymousMethod__0() in WritableBitmapRenderHandler.cs
System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, object, int)
System.Windows.Threading.ExceptionWrapper.TryCatchWhen(object, System.Delegate, object, int, System.Delegate)
System.Windows.Threading.DispatcherOperation.InvokeImpl()
System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(object)
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object, bool)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, object)
MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, object)
...
[Aufrufliste abgeschnitten]

Exception 2

System.Runtime.InteropServices.COMException: "MILERR_WIN32ERROR (Ausnahme von HRESULT: 0x88980003)"
Diese Ausnahme wurde ursprünglich von dieser Aufrufliste ausgelöst:
System.Windows.Media.Imaging.WriteableBitmap.WriteableBitmap(int, int, double, double, System.Windows.Media.PixelFormat, System.Windows.Media.Imaging.BitmapPalette)

On this run I also used VMMap to get information about memory consumption: exception vmmap

Further remarks

It just occurred using this setting:

settings.CefCommandLineArgs.Add("disable-gpu-compositing");

This is a screenshot of taskmanager at the point of time of exception: screenshot taskmanager

SSiefert avatar Apr 12 '21 12:04 SSiefert

A solution having the WPF-project was missing. Without a solution compiling was not possible.

There are two solutions already https://github.com/cefsharp/CefSharp.MinimalExample/blob/master/CefSharp.MinimalExample.sln#L6

https://github.com/cefsharp/CefSharp.MinimalExample/blob/master/CefSharp.MinimalExample.netcore.sln#L8

Only https://github.com/cefsharp/CefSharp.MinimalExample/blob/master/CefSharp.MinimalExample.net472.sln is missing a WPF example, it uses the newer SDK style project, the code is the same.

I set the RenderHandler as described by you in this post: #2035 (comment)

You need to set invalidateDirtyRect to false, your example it's set to true which is the default.

Up to now it just a feeling that it could be directly in CefSharp or WPF since it is similar to #2035 and WebView2 does not have this issue.

Firstly WebView2 isn't an actual WPF control, it's a HwndHost, so it's like hosting the WinForms version in WPF. There's a CefSharp equivalent available at https://github.com/cefsharp/CefSharp.Wpf.HwndHost

Secondly just because the WritablebitmapRenderHandler is the originator of the exception, doesn't mean it's the cause. It's attempting to allocate memory and fails, it doesn't actually mean that it's the source of the leak.

It is possible that it's the cause, you can implement http://cefsharp.github.io/api/89.0.x/html/T_CefSharp_Wpf_IRenderHandler.htm and create a version that all the methods have empty bodies, you won't get an visual updates so update your example to output some text so you know it's still running.

Have you tested on multiple machines with different GPUs? What hardware are you testing on?

I'd also suggest switching to WPF software rendering as a test.

On this run I also used VMMap to get information about memory consumption:

I'm not familiar with VMMap, what's your analysis of the result? Does it differentiate between managed and unmanaged memory?

amaitland avatar Apr 12 '21 20:04 amaitland

I've run your example with minor modification (I've changed to using https://www.google.com/doodles as I've never heard of the Url you have used and I prefer not to open sites that I'm unfamiliar with).

  • Checkout https://github.com/SSiefert/CefSharp.MinimalExample/commit/c078a541f297905c4457756a5d701561f3941c34
  • Open CefSharp.MinimalExample.sln in VS2019
  • Change Url to https://www.google.com/doodles
  • Select CefSharp.MinimalExample.Wpf from dropdown
  • Run (F5)

I've had CefSharp.MinimalExample.Wpf running for 5hrs now and memory usage is stable.

Windows 10 2004 x64 i3-3240 8gb ram Radeon HD 7470 1gb .Net 4.8

If you haven't already please test with a distinctly different set of hardware. It's also possible the website you have used is poorly behaving, please test with https://www.google.com/doodles for reference.

Generally speaking if the leak was directly in CefSharp I'd expect it to reproduce regardless of website and I'd be getting a lot of different users reporting they were experiencing a leak.

amaitland avatar Apr 15 '21 09:04 amaitland

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

no-response[bot] avatar Apr 30 '21 07:04 no-response[bot]