Skip to content

Memory Leak: MainActivity instances are not garbage-collected after destroy #34879

@markusroessler

Description

@markusroessler

Description

MainActivity instances (and everything referenced from them) never get garbage-collected with MAUI 10.0.51.
Last working version: 9.0.120.

Please have a look at: https://github.com/markusroessler/MauiActivityMemoryleak
This sample app does the following:

  • Starts an Android Foreground Service (see MainApplication.OnCreate) - this prevents the Application from terminating when the last Activity closes
  • Periodically invokes the garbage collector (see GCHelper)
  • Monitors the MainActivity instance count (see GCHelper)

Steps to Reproduce

in the console you will see something like:

MauiActivityMemoryleak.GCHelper: Information: Instance count (MauiActivityMemoryleak.MainActivity): 3

A .NET heap dump showed that there are actually no live MainActivity instances. But in the Java heap dump there are still 3 active instance all referenced by JNI GlobalRefs:
Image

Link to public reproduction project repository

https://github.com/markusroessler/MauiActivityMemoryleak

Version with bug

10.0.50

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

9.0.120 SR12

Affected platforms

Android

Affected platform versions

Android 16

Did you find any workaround?

no, even an explicit MainActivity.Dispose() doesn't help.

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    i/regressionThis issue described a confirmed regression on a currently supported versionperf/memory-leak 💦Memory usage grows / objects live forever (sub: perf)platform/androidregressed-in-10-preview1s/triagedIssue has been revieweds/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions