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
[windows] improve memory usage of CollectionView
#16838
Merged
Merged
Conversation
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
Eilon
added
the
area/collectionview 📃
CollectionView, CarouselView, IndicatorView
label
Aug 17, 2023
You said
The code uses Math.Max. Based on your comment, perhaps Math.Min would be the right answer here? |
Context: dotnet#16436 Context: https://github.com/symbiogenesis/Maui.DataGrid/tree/net8-memory-leak A customer sample has a `CollectionView` with 150,000 data-bound rows. Debugging what happens at runtime, I found we effectively were doing: _itemTemplateContexts = new List<ItemTemplateContext>(capacity: 150_000); for (int n = 0; n < 150_000; n++) { _itemTemplateContexts.Add(null); } Then the items were created as you scroll each row into view: if (_itemTemplateContexts[index] == null) { _itemTemplateContexts[index] = context = new ItemTemplateContext(_itemTemplate, _itemsSource[index], _container, _itemHeight, _itemWidth, _itemSpacing, _mauiContext); } return _itemTemplateContexts[index]; This code accesses the indexer multiple times, into a `List<T>` with 150,000 `null` items. To improve this: * use a `Dictionary<int, T>` instead, just let it size dynamically. * use `TryGetValue(..., out var context)`, so each call accesses the indexer one less time than before. * use either the bound collection's size or 64 (whichever is smaller) as a rough estimate of how many might fit on screen at a time. Taking a memory snapshot of the app after startup: Before: Heap Size: 82,899.54 KB After: Heap Size: 81,768.76 KB Which is saving about 1MB of memory on launch. In this case, it feels better to just let the `Dictionary` size itself with an estimate of what `capacity` will be.
jonathanpeppers
force-pushed
the
ItemTemplateContextList
branch
from
August 17, 2023 22:07
ac10c42
to
5019960
Compare
Yes, I tested w/ no capacity and just put the wrong one... Thanks. |
Also related? #15523 |
rmarinho
approved these changes
Aug 21, 2023
We can also ignore the UITests are failing. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
area/collectionview 📃
CollectionView, CarouselView, IndicatorView
memory-leak 💦
Memory usage grows / objects live forever
platform/windows 🪟
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.
Context: #16436
Context: https://github.com/symbiogenesis/Maui.DataGrid/tree/net8-memory-leak
A customer sample has a
CollectionView
with 150,000 data-bound rows.Debugging what happens at runtime, I found we effectively were doing:
Then the items were created as you scroll each row into view:
This code accesses the indexer multiple times, into a
List<T>
with 150,000null
items.To improve this:
use a
Dictionary<int, T>
instead, just let it size dynamically.use
TryGetValue(..., out var context)
, so each call accesses the indexer one less time than before.use either the bound collection's size or 64 (whichever is smaller) as a rough estimate of how many might fit on screen at a time.
Taking a memory snapshot of the app after startup:
Which is saving about 1MB of memory on launch.
In this case, it feels better to just let the
Dictionary
size itself with an estimate of whatcapacity
will be.