Repository navigation
Description
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:
OutOfMemoryErroror- ANR or
- the process gets killed by the system
Android Studio Live Telemetry shows rapid growth in both native and Java heap:
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.IdentityHashTargetsWeakReference<Java.Interop.IJavaPeerable>
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
-
Clone the minimal reproduction repository:
https://github.com/jdw89-123/activitiesFillHeap -
Build and deploy the app to a physical Android device.
-
Launch the app.
-
Repeatedly press the button to switch between ActivityA and ActivityB.
-
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.

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.

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
jdw89-123 commentedon Mar 22, 2026
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.
Stensan commentedon Apr 7, 2026
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:
Results:
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.
Cheesebaron commentedon Apr 8, 2026
@jonpryor suggested on Discord to try CoreCLR instead of Mono Runtime setting
UseMonoRuntimeto 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.
markusroessler commentedon Apr 8, 2026
I am also experiencing this issue. Here's a complete reproduction sample: dotnet/maui#34879
MainActivity instances are not garbage-collected anymore.
Cheesebaron commentedon Apr 8, 2026
Interestingly enough, when I use
dotnet-gcdumpthe 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:
The MainActivity has a button to launch ChildActivity.
dotnet-gcdumpdoes not show ChildActivity retained, whereas the Android Studio profiler sees multiple ChildActivity instances if I launch a ChildActivity and navigate back a few times.markusroessler commentedon Apr 8, 2026
@Cheesebaron yes, i noticed the same but WeakReferences to the MainActivities actually stay alive even though the heap dump doesn't show them.
Cheesebaron commentedon Apr 8, 2026
@markusroessler you are right the WeakReferences to IJavaPeerable keeps growing
jdw89-123 commentedon Apr 8, 2026
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.
jdw89-123 commentedon Apr 10, 2026
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!
markusroessler commentedon Apr 10, 2026
@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.Add stress test for global ref leak during repeated inflation
[Mono.Android] fix global ref leak in TypeManager.Activate
[Mono.Android] fix global ref leak in TypeManager.Activate
[Mono.Android] fix global ref leak in TypeManager.Activate
[Mono.Android] fix global ref leak in TypeManager.Activate
[Java.Interop] fix global ref leak in ConstructPeer
Bump to dotnet/java-interop/dev/peppers/gref-leak; add regression test
Add regression test for global ref leak in LayoutInflater.Inflate
[tests] Add regression test for global ref leak in LayoutInflater.Inf…
[Java.Interop] fix global ref leak in ConstructPeer (dotnet#1403)
[Java.Interop] fix global ref leak in ConstructPeer (#1403)
[tests] Add regression test for global ref leak in LayoutInflater.Inf…
[tests] Add regression test for global ref leak in LayoutInflater.Inf…
[Java.Interop] fix global ref leak in ConstructPeer (#1403)