[Android] add another gc.collect for Android to ensure all is collected (#5847)

* add another gc.collect for Android

* move all gc collects to a helper
This commit is contained in:
Shane Neuville 2019-04-09 18:40:28 -06:00 коммит произвёл Samantha Houts
Родитель 32da34df0e
Коммит cbc9c44297
26 изменённых файлов: 47 добавлений и 58 удалений

Просмотреть файл

@ -43,7 +43,7 @@ namespace Xamarin.Forms.Controls.Issues
{ {
_resultLabel.Text = $"Page2 IsAlive = {_page2Tracker.IsAlive} ({n++})"; _resultLabel.Text = $"Page2 IsAlive = {_page2Tracker.IsAlive} ({n++})";
await Task.Delay(1000); await Task.Delay(1000);
GC.Collect(); GarbageCollectionHelper.Collect();
} }
_resultLabel.Text = $"Page2 IsAlive = {_page2Tracker.IsAlive}"; _resultLabel.Text = $"Page2 IsAlive = {_page2Tracker.IsAlive}";

Просмотреть файл

@ -53,7 +53,7 @@ namespace Xamarin.Forms.Controls.Issues
((Label)((StackLayout)Content).Children [0]).Text = ((Label)((StackLayout)Content).Children [0]).Text =
string.Format ("Page1. But Page2 IsAlive = {0}", _page2Tracker.IsAlive); string.Format ("Page1. But Page2 IsAlive = {0}", _page2Tracker.IsAlive);
await Task.Delay (1000); await Task.Delay (1000);
GC.Collect (); GarbageCollectionHelper.Collect();
} }
} }

Просмотреть файл

@ -78,9 +78,7 @@ namespace Xamarin.Forms.Controls.Issues
AutomationId = "GC", AutomationId = "GC",
Command = new Command(o => Command = new Command(o =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Label.Text = "Counter: " + Counter; Label.Text = "Counter: " + Counter;
}) })
}, },

Просмотреть файл

@ -71,7 +71,7 @@ namespace Xamarin.Forms.Controls.Issues
while (_page2Tracker.IsAlive && _tabTracker.IsAlive && watch.ElapsedMilliseconds < Timeout) while (_page2Tracker.IsAlive && _tabTracker.IsAlive && watch.ElapsedMilliseconds < Timeout)
{ {
await Task.Delay (1000); await Task.Delay (1000);
GC.Collect (); GarbageCollectionHelper.Collect();
} }
watch.Stop(); watch.Stop();

Просмотреть файл

@ -63,8 +63,7 @@ namespace Xamarin.Forms.Controls.Issues
async void Button_Clicked(object sender, EventArgs e) async void Button_Clicked(object sender, EventArgs e)
{ {
Debug.WriteLine(">>>>>>>> Invoking Garbage Collector"); Debug.WriteLine(">>>>>>>> Invoking Garbage Collector");
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
await Navigation.PushAsync(new BugPage()); await Navigation.PushAsync(new BugPage());
} }

Просмотреть файл

@ -93,8 +93,7 @@ namespace Xamarin.Forms.Controls.Issues
void GCbutton_Clicked(object sender, EventArgs e) void GCbutton_Clicked(object sender, EventArgs e)
{ {
System.Diagnostics.Debug.WriteLine(">>>>>>>> Running Garbage Collection"); System.Diagnostics.Debug.WriteLine(">>>>>>>> Running Garbage Collection");
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
System.Diagnostics.Debug.WriteLine($">>>>>>>> GC.GetTotalMemory = {GC.GetTotalMemory(true):n0}"); System.Diagnostics.Debug.WriteLine($">>>>>>>> GC.GetTotalMemory = {GC.GetTotalMemory(true):n0}");
} }

Просмотреть файл

@ -131,9 +131,7 @@ namespace Xamarin.Forms.Controls.Issues
protected override void OnAppearing() protected override void OnAppearing()
{ {
base.OnAppearing(); base.OnAppearing();
GC.Collect(); GarbageCollectionHelper.Collect();
GC.Collect();
GC.Collect();
} }
} }
} }

Просмотреть файл

@ -53,9 +53,7 @@ namespace Xamarin.Forms.Controls.Issues
var collectButton = new Button { Text = "Collect" }; var collectButton = new Button { Text = "Collect" };
collectButton.Clicked += (sender, args) => collectButton.Clicked += (sender, args) =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.Collect();
GC.Collect();
}; };
var startPage = new ContentPage var startPage = new ContentPage

Просмотреть файл

@ -145,9 +145,7 @@ namespace Xamarin.Forms.Controls.Issues
protected override void OnAppearing() protected override void OnAppearing()
{ {
base.OnAppearing(); base.OnAppearing();
GC.Collect(); GarbageCollectionHelper.Collect();
GC.Collect();
GC.Collect();
} }
} }
} }

Просмотреть файл

@ -106,8 +106,7 @@ namespace Xamarin.Forms.Controls.Issues
AutomationId = "GC", AutomationId = "GC",
Command = new Command(o => Command = new Command(o =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
Label.Text = "Counter: " + Counter; Label.Text = "Counter: " + Counter;
}) })
}, },

Просмотреть файл

@ -44,9 +44,7 @@ namespace Xamarin.Forms.Controls.Issues
{ {
public Page2() public Page2()
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Title = "Page2"; Title = "Page2";
System.Diagnostics.Debug.WriteLine("Constructor"); System.Diagnostics.Debug.WriteLine("Constructor");

Просмотреть файл

@ -26,8 +26,7 @@ namespace Xamarin.Forms.Controls.Issues
var gcButton = new Button { Text = "GC", AutomationId = "GC" }; var gcButton = new Button { Text = "GC", AutomationId = "GC" };
gcButton.Clicked += (sender, args) => gcButton.Clicked += (sender, args) =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
if (_44166MDP.Counter > 0) if (_44166MDP.Counter > 0)
{ {

Просмотреть файл

@ -98,8 +98,7 @@ namespace Xamarin.Forms.Controls.Issues
var collect = new Button() { Text = Collect }; var collect = new Button() { Text = Collect };
collect.Clicked += (sender, args) => collect.Clicked += (sender, args) =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
}; };
Title = "Bugzilla 45722"; Title = "Bugzilla 45722";

Просмотреть файл

@ -45,8 +45,7 @@ namespace Xamarin.Forms.Controls.Issues
GCButton.Clicked += (sender, args) => GCButton.Clicked += (sender, args) =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
}; };
MessagingCenter.Subscribe<_45722Label>(this, _45722Label.CountMessage, sender => MessagingCenter.Subscribe<_45722Label>(this, _45722Label.CountMessage, sender =>

Просмотреть файл

@ -51,8 +51,7 @@ namespace Xamarin.Forms.Controls.Issues
}; };
doGC.Clicked += (sender, e) => { doGC.Clicked += (sender, e) => {
GC.Collect (); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
instanceCount.Text = $"Instances: {_45926SecondPage.InstanceCounter.ToString()}"; instanceCount.Text = $"Instances: {_45926SecondPage.InstanceCounter.ToString()}";
messageCount.Text = $"Messages: {_45926SecondPage.MessageCounter.ToString()}"; messageCount.Text = $"Messages: {_45926SecondPage.MessageCounter.ToString()}";
}; };

Просмотреть файл

@ -36,8 +36,7 @@ namespace Xamarin.Forms.Controls.Issues
async void Button_Clicked(object sender, EventArgs e) async void Button_Clicked(object sender, EventArgs e)
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
await Navigation.PushAsync(new ChildPage()); await Navigation.PushAsync(new ChildPage());
} }

Просмотреть файл

@ -52,8 +52,7 @@ namespace Xamarin.Forms.Controls.Issues
var collectButton = new Button { Text = "Garbage", Command = new Command(o => var collectButton = new Button { Text = "Garbage", Command = new Command(o =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
_layout.Children.Add(new Label {Text = "Success"}); _layout.Children.Add(new Label {Text = "Success"});
}) }; }) };
_layout.Children.Add(collectButton); _layout.Children.Add(collectButton);

Просмотреть файл

@ -41,7 +41,7 @@ namespace Xamarin.Forms.Controls.Issues
async void Button_Clicked(object sender, EventArgs e) async void Button_Clicked(object sender, EventArgs e)
{ {
await Navigation.PushAsync(new ListPage()); await Navigation.PushAsync(new ListPage());
GC.Collect(); GarbageCollectionHelper.Collect();
} }
} }

Просмотреть файл

@ -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();
}
}
}

Просмотреть файл

@ -78,9 +78,7 @@ namespace Xamarin.Forms.Controls.Issues
AutomationId = "GC", AutomationId = "GC",
Command = new Command(o => Command = new Command(o =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Label.Text = "Counter: " + Counter; Label.Text = "Counter: " + Counter;
}) })
}, },

Просмотреть файл

@ -78,9 +78,7 @@ namespace Xamarin.Forms.Controls.Issues
AutomationId = "GC", AutomationId = "GC",
Command = new Command(o => Command = new Command(o =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Label.Text = "Counter: " + Counter; Label.Text = "Counter: " + Counter;
}) })
}, },

Просмотреть файл

@ -25,14 +25,14 @@ namespace Xamarin.Forms.Controls.Issues
static Page CreateWeakReferencedPage() static Page CreateWeakReferencedPage()
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
var result = CreatePage(); var result = CreatePage();
s_pageRefs.Add(new WeakReference(result)); s_pageRefs.Add(new WeakReference(result));
// Add a second unreferenced page to prove that the problem only exists // Add a second unreferenced page to prove that the problem only exists
// when pages are actually navigated to/from // when pages are actually navigated to/from
s_pageRefs.Add(new WeakReference(CreatePage())); s_pageRefs.Add(new WeakReference(CreatePage()));
GC.Collect(); GarbageCollectionHelper.Collect();
return result; return result;
} }

Просмотреть файл

@ -26,9 +26,7 @@ namespace Xamarin.Forms.Controls.Issues
static Page CreateWeakReferencedPage() static Page CreateWeakReferencedPage()
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var result = CreatePage(); var result = CreatePage();
s_pageRefs.Add(new WeakReference(result)); s_pageRefs.Add(new WeakReference(result));

Просмотреть файл

@ -50,8 +50,7 @@ namespace Xamarin.Forms.Controls.Issues
Text = "Garbage Collection Things", Text = "Garbage Collection Things",
Command = new Command(() => Command = new Command(() =>
{ {
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
AttachedStateEffects.Clear(); AttachedStateEffects.Clear();
}), }),
}, },
@ -76,16 +75,14 @@ namespace Xamarin.Forms.Controls.Issues
// needed otherwise UWP crashes // needed otherwise UWP crashes
await Task.Delay(100); await Task.Delay(100);
await Navigation.PopAsync(); await Navigation.PopAsync();
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
} }
void OnAllEventsDetached(object sender, EventArgs args) void OnAllEventsDetached(object sender, EventArgs args)
{ {
AttachedStateEffects.Clear(); AttachedStateEffects.Clear();
AttachedStateEffects.AllEventsDetached -= OnAllEventsDetached; AttachedStateEffects.AllEventsDetached -= OnAllEventsDetached;
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
AllEffectsHaveDetached.Text = "Success"; AllEffectsHaveDetached.Text = "Success";
} }

Просмотреть файл

@ -59,6 +59,8 @@ namespace Xamarin.Forms.Controls.Issues
result.Text = "Click 'Push page' again"; result.Text = "Click 'Push page' again";
return; return;
} }
GarbageCollectionHelper.Collect();
result.Text = list[list.Count - 2].IsAlive ? "Failed" : "Success"; result.Text = list[list.Count - 2].IsAlive ? "Failed" : "Success";
}) })
}; };
@ -76,9 +78,7 @@ namespace Xamarin.Forms.Controls.Issues
await Navigation.PushAsync(page); await Navigation.PushAsync(page);
await page.Navigation.PopAsync(); await page.Navigation.PopAsync();
GC.Collect(); GarbageCollectionHelper.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
list.Add(wref); list.Add(wref);
if (list.Count > 1) if (list.Count > 1)

Просмотреть файл

@ -9,6 +9,7 @@
<Import_RootNamespace>Xamarin.Forms.Controls.Issues</Import_RootNamespace> <Import_RootNamespace>Xamarin.Forms.Controls.Issues</Import_RootNamespace>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Helpers\GarbageCollectionHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue5555.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Issue5555.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Bugzilla59172.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Bugzilla59172.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Issue4684.xaml.cs" /> <Compile Include="$(MSBuildThisFileDirectory)Issue4684.xaml.cs" />
@ -1157,4 +1158,4 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator> <Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
</Project> </Project>