Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Add ability to set WebView ExecutionMode property (addresses #4720) #12509

Merged
merged 15 commits into from
Dec 4, 2020

Conversation

t-johnson
Copy link
Contributor

@t-johnson t-johnson commented Oct 16, 2020

Description of Change

Add platform specific to set the ExecutionMode on a WebView on UWP

When set as 'SeparateProcess', the memory allocated by the WebView itself is all handled in a sub-process, which ensures the main process never crashes from running out of memory here from doing things like opening and closing youtube every 5 seconds. This behavior will likely crash when the WebView is in-process due to the huge amounts of memory that is required by that website.

This change is related to issue #4720, however I do not believe that issue is actually a bug. This change does nothing to address a "memory leak" directly, and due to the addition of the new protected member, I wasn't sure if this is classed as an "API change" but thought it would be better to be on the safe side...

Issues Resolved

API Changes

Added:

        public static readonly BindableProperty ExecutionModeProperty = BindableProperty.Create("ExecutionMode", typeof(WebViewExecutionMode), typeof(WebView), WebViewExecutionMode.SameThread);
	public enum WebViewExecutionMode
	{
		SameThread = 0,
		SeparateThread = 1,
		SeparateProcess = 2
	}

Xamarin.Forms.Platform.UWP.WebViewRenderer

  • protected WebViewExecutionMode ExecutionMode

Changed:
None

Removed:
None

Platforms Affected

  • UWP

Behavioral/Visual Changes

None

Before/After Screenshots

Not applicable

Testing Procedure

Simply opening and closing a view that contains a WebView, pointed at something like youtube.com will cause huge memory usage which is not immediately reclaimed. When the WebView is running in SeparateProcess mode, there is a child process that is created that handles the memory itself (see ExecutionMode property

PR Checklist

  • Targets the correct branch
  • Tests are passing (or failures are unrelated)

@t-johnson t-johnson changed the title 4720webview Add ability to set WebView ExecutionMode property (addresses #4720) Oct 16, 2020
@samhouts samhouts added this to In Review in vNext+1 (5.0.0) Nov 2, 2020
@rmarinho rmarinho added this to In progress in v5.0.1 via automation Nov 3, 2020
@rmarinho rmarinho removed this from In Review in vNext+1 (5.0.0) Nov 3, 2020
@rmarinho rmarinho moved this from In progress to Review in progress in v5.0.1 Nov 3, 2020
@rmarinho rmarinho added this to the 5.0.1 milestone Nov 3, 2020
@samhouts samhouts added this to In Review in vNext+1 (5.0.0) Nov 3, 2020
@samhouts samhouts removed this from Review in progress in v5.0.1 Nov 3, 2020
@PureWeen PureWeen added this to In progress in v5.0.1 via automation Nov 5, 2020
@PureWeen PureWeen removed this from In Review in vNext+1 (5.0.0) Nov 5, 2020
@PureWeen PureWeen added this to To do in vNext+1 (5.0.0) via automation Nov 5, 2020
@PureWeen PureWeen removed this from In progress in v5.0.1 Nov 5, 2020
@PureWeen PureWeen modified the milestones: 5.0.1, 5.0.0 Nov 5, 2020
@PureWeen PureWeen added the blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. label Nov 5, 2020
@rmarinho rmarinho moved this from To do to In Review in vNext+1 (5.0.0) Nov 5, 2020
@PureWeen PureWeen removed the request for review from StephaneDelcroix November 16, 2020 14:44
Copy link
Contributor

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this PR is having some issues with Issue12134.CookiesCorrectlyLoadWithMultipleWebViews()

  at Xamarin.Forms.Controls.Issues.Issue12134.CookiesCorrectlyLoadWithMultipleWebViews() in D:\agent\1\s\Xamarin.Forms.Controls.Issues\Xamarin.Forms.Controls.Issues.Shared\Issue12134.cs:line 120

@t-johnson
Copy link
Contributor Author

@PureWeen I'll take a look and see if I can figure out the test failure.

@PureWeen
Copy link
Contributor

@t-johnson

  • I rebased this PR (hopefully that doesn't mess you up)
  • I added some instructions (let me know if those seem correct)

@PureWeen
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@xamarin xamarin deleted a comment from azure-pipelines bot Nov 24, 2020
@PureWeen PureWeen added the DO-NOT-MERGE-!!! 🛑 This is in progress and needs to be updated before it can be merged. label Dec 3, 2020
Copy link
Contributor

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't quite get it to crash even when loading YouTube a bunch of times. I did notice that after awhile it would start to lag. For now I changed the UI Test to just use Microsoft and reload the page ever 200 Ms

Waiting 2 seconds and reloading it 10 times just makes this test a bit too slow. I also just converted this property over to a platform specific so users can opt into it if they want to from the xplat code opposed to a renderer

@PureWeen
Copy link
Contributor

PureWeen commented Dec 3, 2020

/azp run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@PureWeen PureWeen removed the DO-NOT-MERGE-!!! 🛑 This is in progress and needs to be updated before it can be merged. label Dec 3, 2020
Copy link
Contributor Author

@t-johnson t-johnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, much cleaner to have the platform specific

@PureWeen PureWeen added the DO-NOT-MERGE-!!! 🛑 This is in progress and needs to be updated before it can be merged. label Dec 4, 2020
@PureWeen
Copy link
Contributor

PureWeen commented Dec 4, 2020

/azp run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@rmarinho rmarinho merged commit c80a908 into xamarin:5.0.0 Dec 4, 2020
vNext+1 (5.0.0) automation moved this from In Review to Done Dec 4, 2020
pictos pushed a commit to pictos/Xamarin.Forms that referenced this pull request Dec 30, 2020
…4720) (xamarin#12509)

* To address issue xamarin#4720:
Add option for custom renderers to control the execution mode of the WebView control
This is 'opt-in' as by default this commit will not change behavior of existing applciations.  to opt-in, people would need to set the ExecutionMode property in the constructor of their custom WebViewRenderer, like:

  public class MyWebViewRenderer : WebViewRenderer
  {
    public MyWebViewRenderer()
    {
      ExecutionMode = Windows.UI.Xaml.Controls.WebViewExecutionMode.SeparateProcess;
    }
  }

When set as 'SeparateProcess', the memory allocated by the WebView itself is all handled in a sub-process, which ensures the main process never crashes from running out of memory here from doing things like opening and closing youtube every 5 seconds.  This behavior will likely crash when the WebView is in-process due to the huge amounts of memory that is required by that website.

* tabs not spaces

* TemplatedItemsList: Ensure items are unhooked correctly when removing them, whether as individual removals or list resets.  Without this, the native cells do not actually get removed.
CellTableViewCell: use event PropertyChangedEventHandler instead of action (seems more standard)
TextCellRenderer: Correct the event delegate hookup
ListViewRenderer: Actually re-use the UITableViewCell when creating header sections, otherwise we endlessly create new ones, leaving the old ones alive through event handlers

* Revert "TemplatedItemsList: Ensure items are unhooked correctly when removing them, whether as individual removals or list resets.  Without this, the native cells do not actually get removed."

This reverts commit 7da9ffb.

* Make the custom renderer only apply to the test for this issue, as it seems the SeparateProcess affects access to cookies, breaking other tests

* Move additional classes inside test class to decrease namespace cluttering.

* - cleanup and rebase

* - add instructions

* - move WebViewExecutionMode to platform specific

* - fix up UI Test

* - clean up tests

* - fix tabs

* - fix formatting

* - fix teardown call

* - fix async ui test quirk on uwp

Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/performance a/webview blocker Issue blocks next stable release. Prioritize fixing and reviewing this issue. ControlGallery DO-NOT-MERGE-!!! 🛑 This is in progress and needs to be updated before it can be merged. p/UWP partner/cat 😻 t/bug 🐛
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

UWP: Webview: Memory Leak
6 participants