Skip to content

Conversation

@jpobst
Copy link
Contributor

@jpobst jpobst commented Feb 21, 2025

Fixes: #8421
Context: dotnet/java-interop#719
Context: #5278

In dotnet/java-interop#719, we realized we needed to call GC.KeepAlive () on objects that are passed to native code to prevent the GC from collecting them prematurely.

However, this only solved the issue for newly compiled binding libraries and not the existing ecosystem of binding libraries. Thus we added #5278, a "linker" step that would add GC.KeepAlive () calls to existing binding libraries.

The AddKeepAlivesStep runs on all assemblies the contain a reference to Java.Lang.Object, including ones that already have the keep-alives compiled in. Modify the step to skip assemblies that are built against .NET for Android, as we know they have the compiled keep-alives. Technically we could go back to Xamarin.Android 11.1, but using .NET for Android as the cutoff will easily filter out the vast majority of binding libraries.

This results in decent savings for the following test case:

dotnet new android
dotnet add package Xamarin.AndroidX.Activity --version 1.9.3.1
dotnet build -p:AndroidAddKeepAlives=true
Scenario (_LinkAssembliesNoShrink) This PR main
Full 4.19 s 4.72 s

However, in cases like #8421 where this step seems to be taking an abnormal amount of time there should be substantial improvements.

@jpobst jpobst force-pushed the dev/jpobst/fewer-keep-alives branch from 8ffe0b3 to 6dbe7d6 Compare February 21, 2025 19:42
@jpobst jpobst force-pushed the dev/jpobst/fewer-keep-alives branch from 6dbe7d6 to ad6a418 Compare February 21, 2025 21:40
@jpobst jpobst marked this pull request as ready for review February 22, 2025 00:36
@jpobst jpobst merged commit 2c28801 into main Feb 24, 2025
58 checks passed
@jpobst jpobst deleted the dev/jpobst/fewer-keep-alives branch February 24, 2025 18:07
@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LinkAssembliesNoShrink is expensive

4 participants