-
Notifications
You must be signed in to change notification settings - Fork 564
[NativeAOT] Initialize GC threshold much earlier #10488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested on our app, the inital 4 GCs with "outstanding GREFs. Performing a full GC!" message are gone.
jonathanpeppers
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

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.