Bug 55749 - Webviews aren't being garbage collected when the containing page is popped
Summary: Webviews aren't being garbage collected when the containing page is popped
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.4
Hardware: PC Windows
: High major
Target Milestone: ---
Assignee: E.Z. Hart [MSFT]
Depends on:
Reported: 2017-04-28 20:43 UTC by christine.blanda
Modified: 2017-12-20 00:28 UTC (History)
5 users (show)

Tags: android performance memory webview ac
Is this bug a regression?: ---
Last known good build:

Test Project (189.00 KB, application/x-zip-compressed)
2017-04-28 20:46 UTC, christine.blanda
Profiler Results (4.64 MB, application/mlpd)
2017-04-28 20:46 UTC, christine.blanda
Xam support profiler results (2.92 MB, application/x-zip-compressed)
2017-05-01 22:19 UTC, Jimmy [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description christine.blanda 2017-04-28 20:43:00 UTC
In the sample program, ThirdPage contains a webview defined in the xaml.  Nothing is done to the webview in the code.  Run the program and press the ThirdPage button, then the back arrow, 5 times and look at the memory allocations - there are 5 instances of the ThirdPage, WebView, WebViewRenderer, and WebClient (snapshot1 in the attached file).  Not tap the button to garbage collect.  The instances of ThirdPage and WebView drops to one, but WebViewRenderer and WebViewClient are still 5 (snapshot2).  Why aren't these garbage collected?
Comment 1 christine.blanda 2017-04-28 20:46:09 UTC
Created attachment 21894 [details]
Test Project
Comment 2 christine.blanda 2017-04-28 20:46:33 UTC
Created attachment 21895 [details]
Profiler Results
Comment 3 Jimmy [MSFT] 2017-05-01 22:19:57 UTC
Created attachment 21911 [details]
Xam support profiler results

Thank you for filing this report!

I believe I'm seeing the issue when I test with the attached project. I tested with an Android 6.0 device and performed the steps below while taking a snapshot in Profiler after each one. I've attached the results from Profiler, one using 2.3.4 and the other using 2.3.5-pre2.

### Profiling steps

1. Launched the app
2. Press "Open Third Page"
3. Go back
4. Press "Open Third Page"
5. Go back
6. Press "Open Third Page"
7. Go back
8. Press "Garbage Collect"

Each time I navigated to the third page, I could see the number of WebView related object allocations increase and most of the allocations persisted even after the page was popped. After running the GC, the number of allocations for these objects did drop to a single instance as mentioned. 

In contrast, doing the same steps above on iOS results in a similar object allocation while the page is being pushed/popped, but after the GC is ran the objects are completely collected. So this seems to only be affecting Android.
Comment 4 David Ortinau [MSFT] 2017-12-20 00:28:28 UTC
Migrated to https://github.com/xamarin/Xamarin.Forms/issues/1430

Please follow that issue for future updates.