[NativeAOT] Initialize GC threshold much earlier#10488
Merged
jonathanpeppers merged 1 commit intomainfrom Sep 15, 2025
Merged
Conversation
The threshold value is used by the global reference creation
routine to determine whether to [start GC collection][0]:
```csharp
if (gc >= JNIEnvInit.gref_gc_threshold) {
Logger.Log (LogLevel.Debug, "monodroid-gc", gc + " outstanding GREFs. Performing a full GC!");
System.GC.Collect ();
}
```
However, the threshold value was being initialized after we've already invoked some non-trivial
managed code in the managed [NativeAOT runtime][1], too late to avoid at least a handful of
`System.GC.Collect ()` calls:
09-15 14:37:25.923 10761 10761 D monodroid-gc: 1 outstanding GREFs. Performing a full GC!
09-15 14:37:25.923 10761 10761 D monodroid-gc: 2 outstanding GREFs. Performing a full GC!
09-15 14:37:25.923 10761 10761 D monodroid-gc: 3 outstanding GREFs. Performing a full GC!
09-15 14:37:25.924 10761 10761 D monodroid-gc: 3 outstanding GREFs. Performing a full GC!
Fix this by splitting the GC threshold value initialization into a separate method which is
invoked as soon as possible from the `JNI_OnLoad` function exported by the NativeAOT application.
[0]: https://github.com/dotnet/android/blob/e72bf3626024fd1e27dd9cd26d7f8439bc088e19/src/Mono.Android/Android.Runtime/AndroidRuntime.cs#L178-L195
[1]: https://github.com/dotnet/android/blob/e72bf3626024fd1e27dd9cd26d7f8439bc088e19/src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/JavaInteropRuntime.cs#L60
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a timing issue in NativeAOT where the GC threshold was being initialized too late, causing unnecessary garbage collection calls during early runtime initialization.
- Splits GC threshold initialization into a separate method called earlier in the process
- Adds explicit early initialization call in NativeAOT's JNI_OnLoad function
- Removes redundant GC threshold initialization from the main InitializeJniRuntime method
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Mono.Android/Android.Runtime/JNIEnvInit.cs | Extracts GC threshold initialization into new NativeAotInitializeMaxGrefGet method and removes it from InitializeJniRuntime |
| src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/JavaInteropRuntime.cs | Calls the new GC threshold initialization method early in JNI_OnLoad before other initialization |
src/Microsoft.Android.Runtime.NativeAOT/Android.Runtime.NativeAOT/JavaInteropRuntime.cs
Show resolved
Hide resolved
filipnavara
approved these changes
Sep 15, 2025
Member
filipnavara
left a comment
There was a problem hiding this comment.
Tested on our app, the inital 4 GCs with "outstanding GREFs. Performing a full GC!" message are gone.
jonathanpeppers
approved these changes
Sep 15, 2025
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.

The threshold value is used by the global reference creation routine to determine whether to start GC collection:
However, the threshold value was being initialized after we've already invoked some non-trivial managed code in the managed NativeAOT runtime, too late to avoid at least a handful of
System.GC.Collect ()calls:Fix this by splitting the GC threshold value initialization into a separate method which is invoked as soon as possible from the
JNI_OnLoadfunction exported by the NativeAOT application.