Repository navigation
Closed
dotnet/java-interop
#1403Labels
Description
Android framework version
net10.0-android
Affected platform version
VS 18.4.11626.88 & 36.1.30/10.0.100
Description
The LayoutInflater.Inflate method can cause a memory leak if the XML layout contains a reference to a custom .net view. A regression has been observed in net10-android. The same code works correctly in net9-android.
Minimal repro in attachment. The example contains a reference counter. You can switch the project from net10-android to net9-android to see the change in behavior.
net10-android-memory-leak-on-inflate.zip
Steps to Reproduce
- Create new net10-android application
- Add custom view
internal sealed class CustomView : global::Android.Views.View
{
public CustomView(Context? context) : base(context) { }
public CustomView(nint javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }
public CustomView(Context? context, IAttributeSet? attrs) : base(context, attrs) { }
public CustomView(Context? context, IAttributeSet? attrs, int defStyleAttr) : base(context, attrs, defStyleAttr) { }
public CustomView(Context? context, IAttributeSet? attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes) { }
}
- Add layout
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<AndroidApp2.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
- Inflate it's somewhere
LayoutInflater.Inflate(Resource.Layout.test, null, false);
- Observe memory leak
Did you find any workaround?
Nothing.
Activity
anton-yashin commentedon Apr 9, 2026
maybe related to #10989
jonathanpeppers commentedon Apr 13, 2026
How did you "observe" the memory leak?
Was the C#/managed object leaking? or the Java one?
anton-yashin commentedon Apr 14, 2026
Moved repro in repo with dumps included. These dumps not include reference counter via ConditionalWeakTable. I am comment out ReferenceCounter.AddReferenceAndPrint(this) in CustomView before collect dump data.
Difference between managed dotnet-gcdump show there no active references to custom view
Difference between native am dumpheap shows a native references that not got disposed after GC invoke
If there an active reference counter then you can watch in TextView that count of managed references to CustomView going to increase. I don't know how it's works :(
On net9 there no leaks.
Add diagnostic logging for peer lifecycle tracing
Fix race condition: use ConstructPeer before constructor in activation
[Mono.Android] fix global ref leak in TypeManager.Activate (#11101)
[Mono.Android] fix global ref leak in TypeManager.Activate (#11101)
[Mono.Android] fix global ref leak in TypeManager.Activate (#11101)
[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
[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)