Skip to content

Repeated Activity recreation / switching leads to heavy GC pressure, OOM or process kill #10989

Closed
dotnet/java-interop
#1403

Description

@jdw89-123

Android framework version

net10.0-android

Affected platform version

.NET10 android on physical device, Mono runtime

Description

We are seeing what appears to be excessive managed and native memory growth when repeatedly switching between dynamically created Android activities.

In our production app, we use many dynamic activities and frequent activity transitions. Activities are often recreated. To isolate the behavior, I built a minimal reproduction with two simple activities (ActivityA and ActivityB) that navigate back and forth. The UI is created entirely in code.

Minimal reproduction:
https://github.com/jdw89-123/activitiesFillHeap


Expected behavior

Repeated activity recreation and activity switching should remain stable and should not drive the managed or native heap into repeated full-heap GC / OOM scenarios.


Actual behavior

After repeated navigation between activities, the app shows:

  • continuous growth of managed and native memory
  • heavy GC pressure
  • UI stalls
  • eventually either:
    • OutOfMemoryError or
    • ANR or
    • the process gets killed by the system

Android Studio Live Telemetry shows rapid growth in both native and Java heap:

Image

Managed heap analysis

Managed heap dumps (via dotnet-gcdump) show a continuous increase in the number of specific object types.

The object count:

  • does not decrease after GC
  • does not stabilize at a plateau
  • instead grows steadily with repeated activity transitions

Affected types:

  • Android.Runtime.IdentityHashTargets
  • WeakReference<Java.Interop.IJavaPeerable>
Image

Additional observation (explicit GC)

Based on the discussion in the following PR:
#10856

I experimented with triggering an explicit GC in OnCreate of the activity:

System.GC.WaitForPendingFinalizers();
System.GC.Collect();

Notes

The reproduction is intentionally minimal, but it shows the same pattern we are seeing in a much larger production application.

What makes this particularly suspicious is that even this small repro appears sufficient to push both the managed and native heap into a steadily growing state that does not recover over time.

Additionally, the number of view elements on the activities appears to influence how quickly the issue manifests. Increasing the number of UI elements reduces the number of activity transitions required to reach the critical state, suggesting that the problem is related to the amount of UI objects created and destroyed during activity recreation.

Steps to Reproduce

  1. Clone the minimal reproduction repository:
    https://github.com/jdw89-123/activitiesFillHeap

  2. Build and deploy the app to a physical Android device.

  3. Launch the app.

  4. Repeatedly press the button to switch between ActivityA and ActivityB.

  5. Continue switching activities continuously.

After approximately 2–3 minutes of rapid switching, the app enters a critical state and eventually crashes (OutOfMemoryError or process kill).

Did you find any workaround?

no

Relevant log output

Without explicit GC, the repro repeatedly reaches the managed heap limit and eventually throws `OutOfMemoryError`, including failures for very small allocations:

- `Clamp target GC heap from 152MB to 128MB`
- `0% free, 128MB/128MB`
- `Forcing collection of SoftReferences for 32B allocation`
- `Throwing OutOfMemoryError "Failed to allocate a 32 byte allocation ..."`
- stack includes `crc64408c12b9499ddfa7.ActivityA.n_onCreate(Native method)`

With explicit GC, the failure is delayed but not prevented. The app still reaches the heap limit and eventually becomes unresponsive or gets terminated:

- `Activity destroy timeout for ... ActivityB`
- `Clamp target GC heap from 151MB to 128MB`
- `0% free, 127MB/128MB`
- `WaitForGcToComplete blocked Alloc on HeapTrim for 1.603s`
- later: `Throwing OutOfMemoryError "Failed to allocate a 16400 byte allocation ..."`

These logs suggest that explicit GC changes the timing, but not the underlying failure mode.

Full logcats:
logcat1.txt
logcat2.txt


UPDATE: .NET 8 vs .NET 10

I compared the same reproduction on .NET 8 and .NET 10, with explicit GC enabled in OnCreate of both activities:

System.GC.WaitForPendingFinalizers();
System.GC.Collect();

The difference is significant.

Under .NET 8, the application behaves as expected and remains stable during repeated activity switching.
Image

Under .NET 10, memory consumption grows aggressively under the same conditions. Even with explicit GC, the profiler shows clearly higher and continuously increasing memory usage compared to .NET 8.
Image

This suggests that the behavior is not only related to the general activity recreation pattern itself, but that there may also be a regression or behavioral change between .NET 8 and .NET 10 in how Android-managed/native interop objects are retained or released.

Activity

  1. added
    Area: App Runtime
    needs-triage
    on Mar 21, 2026
  2. jdw89-123 commented on Mar 22, 2026

    @jdw89-123
    Author

    Probably related to: #9396

    I am aware that the referenced issue was closed with the argument that explicit GC should mitigate the problem. I tested this as well (even calling GC twice 😵), but this only delays the issue and does not resolve it.

    One interesting observation:

    If I explicitly remove the view elements in OnDestroy, the number of references increases only very slowly (3 IJavaPeerables and 3 IdentityHashTargets per transition).

    However, simply removing the root content layout is not sufficient. It appears necessary to recursively traverse all ViewGroups and explicitly remove each child view individually. If I only remove the content layout, the number of references continues to grow.

    This suggests that the Activity itself may be retained longer than expected and could be the root of the issue, with the associated view hierarchy being kept alive as a consequence.

  3. Stensan commented on Apr 7, 2026

    @Stensan

    I am also encountering this issue. I noticed that JavaFinalize is no longer being invoked for Activity subclasses in .NET 9 and .NET 10, while it worked as expected in .NET 8.

    You can reproduce this behavior with the following steps:

    1. Override the JavaFinalize method in an Activity.
    2. Set a breakpoint inside that method.
    3. Start the app, navigate to the Activity, and then navigate back (or close it) to allow it to be collected.

    Results:

    • .NET 8: The breakpoint is hit eventually (after the GC runs).
    • .NET 9 / .NET 10: The breakpoint is never hit, even after multiple manual GC collections.

    This suggests a potential memory leak, as JavaFinalize should be called by the Garbage Collector once there are no more references to the object. It seems the objects are being kept alive unexpectedly in newer versions. However, I am also open to the possibility that this is an intentional change in .NET 9/10's lifecycle or JNI handling. If this method is being deprecated or replaced by a different mechanism for tracking object lifetime, please let me know.

  4. Cheesebaron commented on Apr 8, 2026

    @Cheesebaron

    @jonpryor suggested on Discord to try CoreCLR instead of Mono Runtime setting UseMonoRuntime to false. However, we still see this issue when using CoreCLR on .NET 10. We also tried .NET 11 preview, which also seems to have this issue.

    We cannot reproduce the issue on .NET 9 though, here the Activities get evicted from memory.

  5. markusroessler commented on Apr 8, 2026

    @markusroessler

    I am also experiencing this issue. Here's a complete reproduction sample: dotnet/maui#34879
    MainActivity instances are not garbage-collected anymore.

  6. Cheesebaron commented on Apr 8, 2026

    @Cheesebaron

    Interestingly enough, when I use dotnet-gcdump the gcdumps do not show the Activity being retained. So from the managed C# side of things all seems good 🤔

    I have a very simple app with 2 activities:

    • MainActivity
    • ChildActivity

    The MainActivity has a button to launch ChildActivity.

    dotnet-gcdump does not show ChildActivity retained, whereas the Android Studio profiler sees multiple ChildActivity instances if I launch a ChildActivity and navigate back a few times.

    Image
  7. markusroessler commented on Apr 8, 2026

    @markusroessler

    @Cheesebaron yes, i noticed the same but WeakReferences to the MainActivities actually stay alive even though the heap dump doesn't show them.

  8. Cheesebaron commented on Apr 8, 2026

    @Cheesebaron

    @markusroessler you are right the WeakReferences to IJavaPeerable keeps growing

    Image
  9. jdw89-123 commented on Apr 8, 2026

    @jdw89-123
    Author

    Thanks for your contributions. That’s exactly what we’ve been observing as well.
    I suspect that unfinalized Java-peer-objects are preventing the activities from being garbage-collected on the native heap (along with everything attached to them).

    Additionally, there appears to be a threshold of approximately 48,000 Java peer objects. Once this limit is reached, garbage collection starts to behave incorrectly. This is illustrated in issue #10856.

  10. jdw89-123 commented on Apr 10, 2026

    @jdw89-123
    Author

    I’m not entirely sure if this is the right GitHub repository for this issue, or if it would be better suited for .NET MAUI.

    This problem is currently having a significant impact on our production environment, so we’d really appreciate any guidance on where to raise it correctly.

    Could someone please point us in the right direction? Maybe @jonathanpeppers or @simonrozsival might have some insight here 🙂

    Thanks a lot!

  11. markusroessler commented on Apr 10, 2026

    @markusroessler

    @jdw89-123 according to this MAUI issue dotnet/maui#34879 the fix is already planned for the .NET 10 SR7 milestone (May 12) - but I would also wish for an earlier fix as this is a pretty severe issue.
    We now had to implement a workaround that restarts our apps when Java.Interop.Runtime.GlobalReferenceCount > 40_000.

  12. locked and limited conversation to collaborators on May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: App Runtimeneeds-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions