mohammad abukhamseh
mohammad abukhamseh

Reputation: 31

Multiple webview2 instances share one renderer process

I have a small WinForms app that opens many forms and each form contains an instance of webview2 control. Inside that control, I load an HTML file that exists on the local hard disk. My question is that for every new form, I create I can see a new renderer process created on the task manager. This new process consumes a lot of memory. Is it possible to share one renderer process between all these instances as they all load the same HTML page?

Upvotes: 0

Views: 1023

Answers (1)

David Risney
David Risney

Reputation: 4377

  1. Ensure you are using the same environment for all of your WebView2s.
  2. Ensure the origin is the same for all of the URIs in your WebView2s.

1. Environment

WebView2 uses the same chromium process model as the Edge and Chrome browsers. When you create a WebView2 it is associated with a CoreWebView2Environment, which you either use explicitly, or if you are using a WebView2 class you can also depend on a default CoreWebView2Environment.

If two WebView2s share the same CoreWebView2Environment then they will share some processes. At least the browser process and some utility processes like the GPU process and network process will be shared.

Renderer processes are shared or not according to the chromium process model heuristics. Usually if the origin of the URI matches between two documents, then the renderer process will be shared.

You can read more about the process model in WebView2 docs.

2. Same origin

To share renderer processes you additionally need the URIs in the WebView2s to have the same origin. However, things other than http and https URIs, like file URIs, or if you are using NavigateToString, or custom URI schemes, may have issues. Instead you can try using CoreWebView2.SetVirtualHostNameToLocalFolderMapping to create virtual https URIs that actually map to local files.

Upvotes: 1

Related Questions