-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Core] Changes disposing items in CollectionView #10590
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...Controls.Sample/Pages/Controls/CollectionViewGalleries/CollectionViewMemoryBenchmark.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<ContentPage | ||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="Maui.Controls.Sample.Pages.CollectionViewGalleries.CollectionViewMemoryBenchmark" | ||
xmlns:local="clr-namespace:Maui.Controls.Sample.Pages.CollectionViewGalleries" | ||
Title="CollectionView Memory Benchmark"> | ||
<ContentPage.Resources> | ||
<ResourceDictionary> | ||
|
||
<Color x:Key="Gray300">#ACACAC</Color> | ||
<SolidColorBrush x:Key="Gray300Brush" Color="{StaticResource Gray300}"/> | ||
|
||
</ResourceDictionary> | ||
</ContentPage.Resources> | ||
<ContentPage.Content> | ||
<Grid | ||
ColumnDefinitions="*,*,*" | ||
ColumnSpacing="5" | ||
RowDefinitions="40,60,20,20,*" | ||
RowSpacing="5"> | ||
|
||
<Button | ||
Grid.Column="0" | ||
Clicked="Button_Clicked_1" | ||
Text="Test 1" /> | ||
<Button | ||
Grid.Column="1" | ||
Clicked="Button_Clicked_2" | ||
Text="Test 2" /> | ||
<Button | ||
Grid.Column="2" | ||
Clicked="Button_Clicked_3" | ||
Text="Test 3" /> | ||
|
||
<Label | ||
Grid.Row="1" | ||
Grid.Column="0" | ||
FontSize="Caption" | ||
HorizontalTextAlignment="Center" | ||
Text="BackgroundColor="{StaticResource Gray300}"" /> | ||
|
||
<Label | ||
Grid.Row="1" | ||
Grid.Column="1" | ||
FontSize="Caption" | ||
HorizontalTextAlignment="Center" | ||
Text="Background="{StaticResource Gray300}"" /> | ||
|
||
<Label | ||
Grid.Row="1" | ||
Grid.Column="2" | ||
FontSize="Caption" | ||
HorizontalTextAlignment="Center" | ||
Text="Background="{StaticResource Gray300Brush}"" /> | ||
|
||
<Label | ||
x:Name="LabelMemory" | ||
Grid.Row="2" | ||
Grid.ColumnSpan="3" | ||
HorizontalTextAlignment="Center" | ||
Text="Memory" /> | ||
|
||
<Label | ||
x:Name="LabelAlive" | ||
Grid.Row="3" | ||
Grid.ColumnSpan="3" | ||
HorizontalTextAlignment="Center" | ||
Text="Objects alive" /> | ||
|
||
<CollectionView | ||
x:Name="CollectionView1" | ||
Grid.Row="4" | ||
Grid.Column="0"> | ||
<CollectionView.ItemTemplate> | ||
<DataTemplate x:DataType="local:DataModel"> | ||
<VerticalStackLayout | ||
local:References.IsWatched="True" | ||
BackgroundColor="{StaticResource Gray300}"> | ||
<Button Text="{Binding ID}" /> | ||
<Label | ||
Margin="10" | ||
Text="{Binding Name}" /> | ||
<Label | ||
Margin="10" | ||
Text="{Binding Data1}" /> | ||
</VerticalStackLayout> | ||
</DataTemplate> | ||
</CollectionView.ItemTemplate> | ||
</CollectionView> | ||
|
||
<CollectionView | ||
x:Name="CollectionView2" | ||
Grid.Row="4" | ||
Grid.Column="1"> | ||
<CollectionView.ItemTemplate> | ||
<DataTemplate x:DataType="local:DataModel"> | ||
<VerticalStackLayout | ||
local:References.IsWatched="True" | ||
Background="{StaticResource Gray300}"> | ||
<Button Text="{Binding ID}" /> | ||
<Label | ||
Margin="10" | ||
Text="{Binding Name}" /> | ||
<Label | ||
Margin="10" | ||
Text="{Binding Data1}" /> | ||
</VerticalStackLayout> | ||
</DataTemplate> | ||
</CollectionView.ItemTemplate> | ||
</CollectionView> | ||
|
||
<CollectionView | ||
x:Name="CollectionView3" | ||
Grid.Row="4" | ||
Grid.Column="2"> | ||
<CollectionView.ItemTemplate> | ||
<DataTemplate x:DataType="local:DataModel"> | ||
<VerticalStackLayout | ||
local:References.IsWatched="True" | ||
Background="{StaticResource Gray300Brush}"> | ||
<Button Text="{Binding ID}" /> | ||
<Label | ||
Margin="10" | ||
Text="{Binding Name}" /> | ||
<Label | ||
Margin="10" | ||
Text="{Binding Data1}" /> | ||
</VerticalStackLayout> | ||
</DataTemplate> | ||
</CollectionView.ItemTemplate> | ||
</CollectionView> | ||
|
||
</Grid> | ||
</ContentPage.Content> | ||
</ContentPage> |
118 changes: 118 additions & 0 deletions
118
...trols.Sample/Pages/Controls/CollectionViewGalleries/CollectionViewMemoryBenchmark.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.Maui.Controls; | ||
|
||
namespace Maui.Controls.Sample.Pages.CollectionViewGalleries | ||
{ | ||
public partial class CollectionViewMemoryBenchmark : ContentPage | ||
{ | ||
int _id; | ||
|
||
public CollectionViewMemoryBenchmark() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
async void Button_Clicked_1(object sender, EventArgs e) | ||
{ | ||
CollectionView1.ItemsSource = GetNewData(); | ||
await UpdateMemoryAsync(1); | ||
} | ||
|
||
async void Button_Clicked_2(object sender, EventArgs e) | ||
{ | ||
CollectionView2.ItemsSource = GetNewData(); | ||
await UpdateMemoryAsync(2); | ||
} | ||
|
||
async void Button_Clicked_3(object sender, EventArgs e) | ||
{ | ||
CollectionView3.ItemsSource = GetNewData(); | ||
await UpdateMemoryAsync(3); | ||
} | ||
|
||
List<DataModel> GetNewData() | ||
{ | ||
List<DataModel> data = new(); | ||
|
||
for (int i = 0; i < 5; i++) | ||
{ | ||
_id += 1; | ||
data.Add(new DataModel() { ID = _id, Name = $"Name{_id}", Data1 = "data1", Data2 = "data2", Data3 = "data3" }); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
async Task UpdateMemoryAsync(int testid) | ||
{ | ||
await Task.Delay(500); | ||
|
||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
LabelMemory.Text = $"Memory {GC.GetTotalMemory(true):N0} after test {testid}"; | ||
LabelAlive.Text = $"Objects alive {References.GetAliveCount()}"; | ||
|
||
References.Print(); | ||
} | ||
} | ||
|
||
public class DataModel | ||
{ | ||
public int ID { get; set; } | ||
public string Name { get; set; } | ||
public string Data1 { get; set; } | ||
public string Data2 { get; set; } | ||
public string Data3 { get; set; } | ||
} | ||
|
||
public static class References | ||
{ | ||
static readonly List<WeakReference> Refs = new(); | ||
|
||
public static readonly BindableProperty IsWatchedProperty = | ||
BindableProperty.CreateAttached("IsWatched", typeof(bool), typeof(References), false, propertyChanged: OnIsWatchedChanged); | ||
|
||
public static bool GetIsWatched(BindableObject obj) | ||
{ | ||
return (bool)obj.GetValue(IsWatchedProperty); | ||
} | ||
|
||
public static void SetIsWatched(BindableObject obj, bool value) | ||
{ | ||
obj.SetValue(IsWatchedProperty, value); | ||
} | ||
|
||
static void OnIsWatchedChanged(BindableObject bindable, object oldValue, object newValue) | ||
{ | ||
AddRef(bindable); | ||
} | ||
|
||
public static void AddRef(object p) | ||
{ | ||
Refs.Add(new WeakReference(p)); | ||
} | ||
|
||
public static void Print() | ||
{ | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
Refs.RemoveAll(a => !a.IsAlive); | ||
foreach (WeakReference weakReference in Refs) | ||
Debug.WriteLine("IsAlive: " + weakReference.Target?.GetType().Name); | ||
|
||
Debug.WriteLine($"Total Refs still alive: {Refs.Count}"); | ||
Debug.WriteLine("---------------"); | ||
} | ||
|
||
public static int GetAliveCount() | ||
{ | ||
return Refs.Count(weakReference => weakReference.IsAlive); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you remove all the
dispose
code does that still work?I don't think we want to start pulling dispose patterns in the code here.