[Android] add another gc.collect for Android to ensure all is collected (#5847)
authorShane Neuville <shane94@hotmail.com>
Wed, 10 Apr 2019 00:40:28 +0000 (18:40 -0600)
committerSamantha Houts <samhouts@users.noreply.github.com>
Wed, 10 Apr 2019 00:40:28 +0000 (17:40 -0700)
* add another gc.collect for Android

* move all gc collects to a helper

26 files changed:
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla31145.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla31255.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla32206.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla32898.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla35132.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla39489.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla40955.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla42069.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla42329.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla43941.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44047.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44166.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45722.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45722Xaml0.xaml.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45926.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla51503.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55365.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla57910.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Helpers/GarbageCollectionHelper.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1023.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1024.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1703.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue1895.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2399.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue5555.cs
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems

index 2fd9f9a..d2e04b7 100644 (file)
@@ -43,7 +43,7 @@ namespace Xamarin.Forms.Controls.Issues
                        {
                                _resultLabel.Text = $"Page2 IsAlive = {_page2Tracker.IsAlive} ({n++})";
                 await Task.Delay(1000);
-                               GC.Collect();
+                               GarbageCollectionHelper.Collect();
                        }
 
                        _resultLabel.Text = $"Page2 IsAlive = {_page2Tracker.IsAlive}";
index 896039c..a1f8718 100644 (file)
@@ -53,7 +53,7 @@ namespace Xamarin.Forms.Controls.Issues
                                ((Label)((StackLayout)Content).Children [0]).Text =
                                                string.Format ("Page1. But Page2 IsAlive = {0}", _page2Tracker.IsAlive);
                                await Task.Delay (1000);
-                               GC.Collect ();
+                               GarbageCollectionHelper.Collect();
                        }
                }
 
index 428538f..a6c677e 100644 (file)
@@ -78,9 +78,7 @@ namespace Xamarin.Forms.Controls.Issues
                                                AutomationId = "GC",
                                                Command = new Command(o =>
                                                {
-                                                       GC.Collect();
-                                                       GC.WaitForPendingFinalizers();
-                                                       GC.Collect();
+                                                       GarbageCollectionHelper.Collect();
                                                        Label.Text = "Counter: " + Counter;
                                                })
                                        },
index 727b167..02f63d1 100644 (file)
@@ -71,7 +71,7 @@ namespace Xamarin.Forms.Controls.Issues
                        while (_page2Tracker.IsAlive && _tabTracker.IsAlive && watch.ElapsedMilliseconds < Timeout) 
                        {
                                await Task.Delay (1000);
-                               GC.Collect ();
+                               GarbageCollectionHelper.Collect();
                        }
 
                        watch.Stop();
index fddc247..714c183 100644 (file)
@@ -63,8 +63,7 @@ namespace Xamarin.Forms.Controls.Issues
                        async void Button_Clicked(object sender, EventArgs e)
                        {
                                Debug.WriteLine(">>>>>>>> Invoking Garbage Collector");
-                               GC.Collect();
-                               GC.WaitForPendingFinalizers();
+                               GarbageCollectionHelper.Collect();
 
                                await Navigation.PushAsync(new BugPage());
                        }
index 1cc68fa..1a666d9 100644 (file)
@@ -93,8 +93,7 @@ namespace Xamarin.Forms.Controls.Issues
                void GCbutton_Clicked(object sender, EventArgs e)
                {
                        System.Diagnostics.Debug.WriteLine(">>>>>>>> Running Garbage Collection");
-                       GC.Collect();
-                       GC.WaitForPendingFinalizers();
+                       GarbageCollectionHelper.Collect();
                        System.Diagnostics.Debug.WriteLine($">>>>>>>> GC.GetTotalMemory = {GC.GetTotalMemory(true):n0}");
                }
 
index 543a4ca..e98990f 100644 (file)
@@ -131,9 +131,7 @@ namespace Xamarin.Forms.Controls.Issues
                        protected override void OnAppearing()
                        {
                                base.OnAppearing();
-                               GC.Collect();
-                               GC.Collect();
-                               GC.Collect();
+                               GarbageCollectionHelper.Collect();
                        }
                }
        }
index 989bbe7..272afc3 100644 (file)
@@ -53,9 +53,7 @@ namespace Xamarin.Forms.Controls.Issues
                        var collectButton = new Button { Text = "Collect" };
                        collectButton.Clicked += (sender, args) =>
                        {
-                               GC.Collect();
-                               GC.Collect();
-                               GC.Collect();
+                               GarbageCollectionHelper.Collect();
                        };
 
                        var startPage = new ContentPage
index 74ffbbf..0a46e22 100644 (file)
@@ -145,9 +145,7 @@ namespace Xamarin.Forms.Controls.Issues
                        protected override void OnAppearing()
                        {
                                base.OnAppearing();
-                               GC.Collect();
-                               GC.Collect();
-                               GC.Collect();
+                               GarbageCollectionHelper.Collect();
                        }
                }
        }
index 9c6d9b0..3af8636 100644 (file)
@@ -106,8 +106,7 @@ namespace Xamarin.Forms.Controls.Issues
                                                AutomationId = "GC",
                                                Command = new Command(o =>
                                                {
-                                                       GC.Collect();
-                                                       GC.WaitForPendingFinalizers();
+                                                       GarbageCollectionHelper.Collect();
                                                        Label.Text = "Counter: " + Counter;
                                                })
                                        },
index 7845013..139fcee 100644 (file)
@@ -44,9 +44,7 @@ namespace Xamarin.Forms.Controls.Issues
        {
                public Page2()
                {
-                       GC.Collect();
-                       GC.WaitForPendingFinalizers();
-                       GC.Collect();
+                       GarbageCollectionHelper.Collect();
 
                        Title = "Page2";
                        System.Diagnostics.Debug.WriteLine("Constructor");
index 85c168c..89f6924 100644 (file)
@@ -26,8 +26,7 @@ namespace Xamarin.Forms.Controls.Issues
                        var gcButton = new Button { Text = "GC", AutomationId = "GC" };
                        gcButton.Clicked += (sender, args) =>
                        {
-                               GC.Collect();
-                               GC.WaitForPendingFinalizers();
+                               GarbageCollectionHelper.Collect();
 
                                if (_44166MDP.Counter > 0)
                                {
index 2009291..53265ba 100644 (file)
@@ -98,8 +98,7 @@ namespace Xamarin.Forms.Controls.Issues
                        var collect = new Button() { Text = Collect };
                        collect.Clicked += (sender, args) =>
                        {
-                               GC.Collect();
-                               GC.WaitForPendingFinalizers();
+                               GarbageCollectionHelper.Collect();
                        };
 
                        Title = "Bugzilla 45722";
index 25afd55..4ca1cff 100644 (file)
@@ -45,8 +45,7 @@ namespace Xamarin.Forms.Controls.Issues
 
                        GCButton.Clicked += (sender, args) =>
                        {
-                               GC.Collect();
-                               GC.WaitForPendingFinalizers();
+                               GarbageCollectionHelper.Collect();
                        };
 
                        MessagingCenter.Subscribe<_45722Label>(this, _45722Label.CountMessage, sender =>
index 4427f39..e1130f9 100644 (file)
@@ -51,8 +51,7 @@ namespace Xamarin.Forms.Controls.Issues
                        };
 
                        doGC.Clicked += (sender, e) => {
-                               GC.Collect ();
-                               GC.WaitForPendingFinalizers();
+                               GarbageCollectionHelper.Collect();
                                instanceCount.Text = $"Instances: {_45926SecondPage.InstanceCounter.ToString()}";
                                messageCount.Text = $"Messages: {_45926SecondPage.MessageCounter.ToString()}";
                        };
index 875d134..37a98e1 100644 (file)
@@ -36,8 +36,7 @@ namespace Xamarin.Forms.Controls.Issues
 
                        async void Button_Clicked(object sender, EventArgs e)
                        {
-                               GC.Collect();
-                               GC.WaitForPendingFinalizers();
+                               GarbageCollectionHelper.Collect();
 
                                await Navigation.PushAsync(new ChildPage());
                        }
index 1ab99a2..161c965 100644 (file)
@@ -52,8 +52,7 @@ namespace Xamarin.Forms.Controls.Issues
 
                        var collectButton = new Button { Text = "Garbage", Command = new Command(o =>
                        {
-                               GC.Collect();
-                               GC.WaitForPendingFinalizers();
+                               GarbageCollectionHelper.Collect();
                                _layout.Children.Add(new Label {Text = "Success"});
                        }) };
                        _layout.Children.Add(collectButton);
index ffc9eb9..5c7af03 100644 (file)
@@ -41,7 +41,7 @@ namespace Xamarin.Forms.Controls.Issues
                        async void Button_Clicked(object sender, EventArgs e)
                        {
                                await Navigation.PushAsync(new ListPage());
-                               GC.Collect();
+                               GarbageCollectionHelper.Collect();
                        }
                }
 
diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Helpers/GarbageCollectionHelper.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Helpers/GarbageCollectionHelper.cs
new file mode 100644 (file)
index 0000000..f5a4299
--- /dev/null
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       public static class GarbageCollectionHelper
+       {
+               public static void Collect()
+               {
+                       GC.Collect();
+                       GC.WaitForPendingFinalizers();
+                       GC.Collect();
+               }
+       }
+}
index a09a49a..e1c27dc 100644 (file)
@@ -78,9 +78,7 @@ namespace Xamarin.Forms.Controls.Issues
                                                AutomationId = "GC",
                                                Command = new Command(o =>
                                                {
-                                                       GC.Collect();
-                                                       GC.WaitForPendingFinalizers();
-                                                       GC.Collect();
+                                                       GarbageCollectionHelper.Collect();
                                                        Label.Text = "Counter: " + Counter;
                                                })
                                        },
index f62369c..20744de 100644 (file)
@@ -78,9 +78,7 @@ namespace Xamarin.Forms.Controls.Issues
                                                AutomationId = "GC",
                                                Command = new Command(o =>
                                                {
-                                                       GC.Collect();
-                                                       GC.WaitForPendingFinalizers();
-                                                       GC.Collect();
+                                                       GarbageCollectionHelper.Collect();
                                                        Label.Text = "Counter: " + Counter;
                                                })
                                        },
index ec9bf17..eaf2569 100644 (file)
@@ -25,14 +25,14 @@ namespace Xamarin.Forms.Controls.Issues
 
                static Page CreateWeakReferencedPage()
                {
-                       GC.Collect();
+                       GarbageCollectionHelper.Collect();
                        var result = CreatePage();
                        s_pageRefs.Add(new WeakReference(result));
 
                        // Add a second unreferenced page to prove that the problem only exists
                        // when pages are actually navigated to/from
                        s_pageRefs.Add(new WeakReference(CreatePage()));
-                       GC.Collect();
+                       GarbageCollectionHelper.Collect();
                        return result;
                }
 
index ce0b311..926c3b2 100644 (file)
@@ -26,9 +26,7 @@ namespace Xamarin.Forms.Controls.Issues
 
                static Page CreateWeakReferencedPage()
                {
-                       GC.Collect();
-                       GC.WaitForPendingFinalizers();
-                       GC.Collect();
+                       GarbageCollectionHelper.Collect();
 
                        var result = CreatePage();
                        s_pageRefs.Add(new WeakReference(result));
index 5116053..0ec91f0 100644 (file)
@@ -50,8 +50,7 @@ namespace Xamarin.Forms.Controls.Issues
                                                                Text = "Garbage Collection Things",
                                                                Command = new Command(() =>
                                                                {
-                                                                       GC.Collect();
-                                                                       GC.WaitForPendingFinalizers();
+                                                                       GarbageCollectionHelper.Collect();
                                                                        AttachedStateEffects.Clear();
                                                                }),
                                                        },
@@ -76,16 +75,14 @@ namespace Xamarin.Forms.Controls.Issues
                        // needed otherwise UWP crashes
                        await Task.Delay(100);
                        await Navigation.PopAsync();
-                       GC.Collect();
-                       GC.WaitForPendingFinalizers();
+                       GarbageCollectionHelper.Collect();
                }
 
                void OnAllEventsDetached(object sender, EventArgs args)
                {
                        AttachedStateEffects.Clear();
                        AttachedStateEffects.AllEventsDetached -= OnAllEventsDetached;
-                       GC.Collect();
-                       GC.WaitForPendingFinalizers();
+                       GarbageCollectionHelper.Collect();
                        AllEffectsHaveDetached.Text = "Success";
                }
 
index c38832b..0fc76b4 100644 (file)
@@ -59,6 +59,8 @@ namespace Xamarin.Forms.Controls.Issues
                                                result.Text = "Click 'Push page' again";
                                                return;
                                        }
+
+                                       GarbageCollectionHelper.Collect();
                                        result.Text = list[list.Count - 2].IsAlive ? "Failed" : "Success";
                                })
                        };
@@ -76,9 +78,7 @@ namespace Xamarin.Forms.Controls.Issues
                                                        await Navigation.PushAsync(page);
                                                        await page.Navigation.PopAsync();
 
-                                                       GC.Collect();
-                                                       GC.WaitForPendingFinalizers();
-                                                       GC.Collect();
+                                                       GarbageCollectionHelper.Collect();
 
                                                        list.Add(wref);
                                                        if (list.Count > 1)
index e0766d8..a1e6c59 100644 (file)
@@ -9,6 +9,7 @@
     <Import_RootNamespace>Xamarin.Forms.Controls.Issues</Import_RootNamespace>
   </PropertyGroup>
   <ItemGroup>
+    <Compile Include="$(MSBuildThisFileDirectory)Helpers\GarbageCollectionHelper.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue5555.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla59172.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue4684.xaml.cs" />
       <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
     </EmbeddedResource>
   </ItemGroup>
-</Project>
+</Project>
\ No newline at end of file