runtime: Memory leak in .Net 8 using Entity Framework and Sqllite (.Net Android)

Android framework version

net8.0-android, net9.0-android

Affected platform version

.NET 8 & .NET 9

Description

Hi,

We have been running our .Net Maui Android app on .Net 7 for some time now and upgraded to .Net 8.

After the upgrade, we realized that the app crashes after a few hours of use.

I have narrowed down the culprit to EF call on Sqllite. I am able to induce this problem by just reading from a table. See below:

private async Task TesterLoop()
    {
        while (true)
        {
            var testDataMessages = await GetTestDataMessages(new CancellationToken());
        }
    }

private async Task<List<TestData>> GetTestDataMessages(CancellationToken cancellationToken)
    {
	await using var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken);

        List<TestData> returnResult = await dbContext.TestData.AsNoTracking()
            .ToListAsync(cancellationToken);

        await dbContext.DisposeAsync().ConfigureAwait(false);

        return returnResult;
 }

Please see attached zip of test application where you can clearly see the memory rise in .Net 8 and not using .Net 7.

See below memory usage of .Net 7 app after 10 min. image

See below memory usage of .Net 8 app after 10 min. image

See below test application.

TestMaui.zip

Steps to Reproduce

  1. Run Test.Maui.7 on Android device
  2. Notice memory stays constant
  3. Run Test.Maui.8 on Android device
  4. Notice native memory climb fast

Did you find any workaround?

No

Relevant log output

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 21 (13 by maintainers)

Most upvoted comments

I’ve been looking at TransactionScope (and ConditionalWeakTable and finalizers) and have at least a partial explanation. I’ll try to write it up later today.

TestMaui.zip @BrzVlad - Please see updated test application. It now includes the following projects: Test.Maui.7 -> DotNet 7 Maui app (no leak) Test.Maui.8 -> DotNet 8 Maui app (leak detected) Test.Android.7 -> DotNet 7 Android app (no leak) Test.Android.8 -> DotNet 8 Android app (leak detected) Test.Console.8 -> DotNet 8 Console app (no leak)

Please let me know if there’s anything else I can provide to assist with the investigation.

@julealgon #50683 indeed seems like a duplicate of this. However, as with many “GC isn’t working” issues that users open, I can’t see any evidence of a leak - the complaint is that memory usage is growing, not that there’s an actual OutOfMemoryException. So it may very well be that TransactionScopeAsyncFlowOption.Enabled causes increased memory usage, but that by no means implies a leak - the GC may simply not be reclaiming the memory yet (but would as memory usage approaches the limit). This is a point we keep need to explain to users.