You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Memory leak: Android.Runtime.IdentityHashTargets and WeakReference<IJavaPeerable> when navigating between two or more activities #9396
I noticed this issue on .NET MAUI, and was able to track it down to here, .NET Android Runtime. There appears to be a memory leak when navigating between pages. I created a second activity with a button that when clicked calls Finish(), then in the MainActivity there's another button that navigates to the second activity.
App init, no navigations:
After about 100 navigations:
Steps to Reproduce
Create a new .NET 8 Android application
In activity_main.xml add a button
In MainActivity.cs add the following code to the bottom of OnCreate():
Button buttonToSecondActivity = FindViewById<Button>(Resource.Id.button);buttonToSecondActivity.Click += (sender, e) =>{ var intent = new Intent(this, typeof(SecondActivity)); StartActivity(intent);};
Create a new XML layout called second_activity.xml , and add a button to the page
Create a new activity called SecondActivity and at the bottom of OnCreate() add the following code:
~Object ()
{
// FIXME: need hash cleanup mechanism.
// Finalization occurs after a test of java persistence. If the
// handle still contains a java reference, we can't finalize the
// object and should "resurrect" it.
refs_added = 0;
if (Environment.HasShutdownStarted) {
return;
}
JniEnvironment.Runtime.ValueManager.FinalizePeer (this);
}
That being said, I've seen IdentityHashTargets in diffs of .gcdump files and wondered if there was an issue there. Is that the only type you see a problem with? What if you add a GC.Collect() call in the sample?
Every Java.Lang.Object subclass holds a JNI Global Reference to its corresponding Java peer. This JNI Global Reference prevents the Android VM from collecting those instances.
When .NET performs a GC, any Java.Lang.Object subclass instances will be added to the .NET GC Finalization Queue, but not finalized.
During a subsequent GC, all Java.Lang.Object subclasses in the finalization queue are passed to the "GC Bridge", which:
"Copies over" the object graph references of (2) to their corresponding Java peers.
"Toggles" the JNI Global References to JNI Weak Global References.
Kicks off a Java-side GC
"Toggles back" the JNI Weak Global References to JNI Global References
If this "toggle back" results in the NULL pointer, the Java peer was collected, and we inform the .NET GC that the managed peer cal be collected.
If this "toggle back" results in a non-NULL pointer, then the Java peer is still alive, and we inform the .NET GC that the managed peer cannot be collected.
Applying these broad strokes to your sample app:
The AndroidApp2.MainActivity C# instance keeps the Java peer crc64….MainActivity alive.
The AndroidApp2.MainActivity instance will not be collected until (1) multiple .NET GC's occur in which (2) the instance is added to the finalization queue, and (3) the instance is passed to the GC Bridge.
This is likely at least two GCs.
Your app doesn't cause any GCs, at least until you've navigated between the pages a lot.
If this is a concern, there are two things you can do:
Have a GC.Collect() invocation somewhere in your Android lifecycle code.
(Optional) call this.Dispose() within your MainActivity instance when you know that instance will no longer be used from Java code.
@mattsetaro we don't think there is an issue here, but let us know if you have other details. If you can add GC.Collect() calls in your app, and still see things never going away, we could investigate.
Activity
mattsetaro commentedon Oct 15, 2024
I can't help but think its related to this:
jonathanpeppers commentedon Nov 1, 2024
@mattsetaro your sample needs to call
GC.Collect()at least once in there to be sure it's running in a determistic way:That being said, I've seen
IdentityHashTargetsin diffs of.gcdumpfiles and wondered if there was an issue there. Is that the only type you see a problem with? What if you add aGC.Collect()call in the sample?jonpryor commentedon Nov 4, 2024
I believe that this is less a "bug" and more a "this is how things work (for better or worse)."
See also the Xamarin.Android Garbage Collection documentation (which really should be migrated to .NET for Android…)
In broad strokes:
Every
Java.Lang.Objectsubclass holds a JNI Global Reference to its corresponding Java peer. This JNI Global Reference prevents the Android VM from collecting those instances.When .NET performs a GC, any
Java.Lang.Objectsubclass instances will be added to the .NET GC Finalization Queue, but not finalized.During a subsequent GC, all
Java.Lang.Objectsubclasses in the finalization queue are passed to the "GC Bridge", which:If this "toggle back" results in the
NULLpointer, the Java peer was collected, and we inform the .NET GC that the managed peer cal be collected.If this "toggle back" results in a non-
NULLpointer, then the Java peer is still alive, and we inform the .NET GC that the managed peer cannot be collected.Applying these broad strokes to your sample app:
AndroidApp2.MainActivityC# instance keeps the Java peercrc64….MainActivityalive.AndroidApp2.MainActivityinstance will not be collected until (1) multiple .NET GC's occur in which (2) the instance is added to the finalization queue, and (3) the instance is passed to the GC Bridge.This is likely at least two GCs.
If this is a concern, there are two things you can do:
GC.Collect()invocation somewhere in your Android lifecycle code.this.Dispose()within yourMainActivityinstance when you know that instance will no longer be used from Java code.jonathanpeppers commentedon Nov 4, 2024
@mattsetaro we don't think there is an issue here, but let us know if you have other details. If you can add
GC.Collect()calls in your app, and still see things never going away, we could investigate.