[Mono.Android] Mark JavaFinalize() as [Obsolete]#11424
Open
jonathanpeppers wants to merge 2 commits into
Open
Conversation
Java's Object.finalize() has been deprecated since JDK 9 and is marked for removal. The correct pattern for cleanup in .NET Android is to override Dispose(bool disposing) or use the C# destructor, not JavaFinalize(). Overriding JavaFinalize() can also cause issues where the Java GC calls finalize() on a Java wrapper even when C# still holds a live reference to the managed object, leading to ObjectDisposedException (see #2072). Mark JavaFinalize() as [Obsolete] via binding metadata to guide users toward the correct disposal patterns. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Marks the managed binding for java.lang.Object.finalize() (JavaFinalize()) as deprecated so consumers are guided away from overriding Java finalization and toward the recommended .NET Android cleanup patterns.
Changes:
- Add a deprecation message for
java.lang.Object.finalize()in binding metadata (intended to surface as[Obsolete]onJavaFinalize()).
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
simonrozsival
approved these changes
May 20, 2026
Member
Author
|
Let's wait to merge this when this lands in dotnet/maui/net11.0 branch: If we merge now, the MAUI integration lane will fail. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Java's
Object.finalize()has been deprecated since JDK 9 and is marked for removal. The correct pattern for cleanup in .NET Android is to overrideDispose(bool disposing)or use the C# destructor, notJavaFinalize().Overriding
JavaFinalize()can also cause issues where the Java GC callsfinalize()on a Java wrapper even when C# still holds a live reference to the managed object, leading toObjectDisposedException(see #2072).Changes
Object.JavaFinalize()as[Obsolete]via binding metadata to guide users toward the correct disposal patterns.Companion PR
GenericAnimatorListenerto useDispose(bool)instead ofJavaFinalize()Closes #10192