Skip to content

[tests] Enable back some MemoryLeak tests#28484

Open
rmarinho wants to merge 14 commits intomainfrom
fix-27411
Open

[tests] Enable back some MemoryLeak tests#28484
rmarinho wants to merge 14 commits intomainfrom
fix-27411

Conversation

@rmarinho
Copy link
Copy Markdown
Member

@rmarinho rmarinho commented Mar 18, 2025

Description of Change

Enable back comment out memory leak tests that were failing on Android on the net10.0 branch

Issues Fixed

Fixes #27411

@rmarinho rmarinho added area-testing Unit tests, device tests perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf) testing-reenable labels Mar 18, 2025
@rmarinho rmarinho added this to the .NET 10.0-preview3 milestone Mar 18, 2025
@rmarinho rmarinho requested a review from a team as a code owner March 18, 2025 18:58
@rmarinho rmarinho moved this from Todo to Ready To Review in MAUI SDK Ongoing Mar 18, 2025
//https://github.com/dotnet/maui/issues/27411
#endif
[InlineData(typeof(TabbedPage))]
public async Task PagesDoNotLeak(Type type)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is failing on Android Api Level 23:
image

Reference to Microsoft.Maui.Controls.NavigationPage (type Microsoft.Maui.Controls.NavigationPage is still alive.\nReference to androidx.fragment.app.FragmentContainerView{38d3432 V.E...... ......ID 0,0-1080,1773 #7f080142 app:id/nav_host} (type AndroidX.Fragment.App.FragmentContainerView is still alive.\n\n---- Assertion timed out

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it works on newer emulators, you might just skip this test on API 23. It probably isn't a leak. There is at least one example of that in here somewhere.

@github-project-automation github-project-automation bot moved this from Ready To Review to Changes Requested in MAUI SDK Ongoing Mar 19, 2025
@rmarinho rmarinho linked an issue Mar 19, 2025 that may be closed by this pull request
@rmarinho
Copy link
Copy Markdown
Member Author

/rebase

@rmarinho
Copy link
Copy Markdown
Member Author

/rebase

if (m == 1)
{
GC.Collect();
GC.Collect(2);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the 2 do?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the number of "generations".

If making a higher number makes the test pass, this change is fine. 👍

But I would say we should move it to a single test utility method for all the tests to share.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, GC.Collect() should work better, because it will run on all generations. with 2 it will go from gen0 to gen2

Copy link
Copy Markdown
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make a TestUtilities.cs that all the tests share? Then for now a single method like:

public static async Task WaitForGC ()
{
	await Task.Yield();
	GC.Collect(2);
	GC.WaitForPendingFinalizers();
}

Then if you wanted to change the number 2 later (or other logic), it could be updated in one place.

Comment on lines 629 to 631
await Task.Yield();
GC.Collect();
GC.Collect(2);
GC.WaitForPendingFinalizers();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there are many permutations of code like this littered thoughout the tests.

Can we make a single TestUtilities.cs file we can share between all the test projects? You shouldn't have to update so many files to introduce the number 2.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

@pictos pictos Jan 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good approach to call GC.Collect() after GC.WaitForPendingFinalizers(); so the objects that had a finalizer will be collected.

Just a heads up, objects that have a Finalizer will need 2 collections to get collected, the first one it will be promoted to the next generation and will be referenced by the finalizer_queue until its Finalizer runs, then a second collection will collect those objects

@dotnet dotnet deleted a comment from azure-pipelines bot Jan 22, 2026
@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@rmarinho
Copy link
Copy Markdown
Member Author

/azp run maui-pr-devicetests

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

# Conflicts:
#	src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Bugzilla/Bugzilla42329.cs
# Conflicts:
#	src/Controls/tests/Xaml.UnitTests/Issues/Maui22036.xaml.cs
- Suppress CA1307 analyzer warnings for LastIndexOf(char) calls - char overload doesn't have StringComparison parameter
- Fix Assert.IsNull to Assert.Null in Maui22036.xaml.cs (xUnit uses Assert.Null, not Assert.IsNull)
- Suppress CA2252 in Xaml.UnitTests.csproj for preview feature usage in generated code
Make the test more robust by retrying GC collection up to 10 times
with delays between attempts. Android's GC can be less aggressive
and may not immediately run finalizers after a single GC.Collect() call.
Apply the same retry pattern to Bugzilla40955 and Bugzilla44166 tests
to make them more robust on Android where GC may not immediately
run finalizers.
Updated garbage collection method to use default settings.
@MauiBot
Copy link
Copy Markdown
Collaborator

MauiBot commented Mar 23, 2026

⚠️ Merge Conflict Detected — This PR has merge conflicts with its target branch. Please rebase onto the target branch and resolve the conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-testing Unit tests, device tests perf/memory-leak 💦 Memory usage grows / objects live forever (sub: perf) platform/android testing-reenable

Projects

Status: Changes Requested

Development

Successfully merging this pull request may close these issues.

[net10.0] Enable back Memory leak tests on Android

9 participants