-
Notifications
You must be signed in to change notification settings - Fork 480
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
Use the new .NET 8 APIs to configure max heap memory size #1578
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,7 +114,11 @@ private LambdaBootstrap(HttpClient httpClient, LambdaBootstrapHandler handler, L | |
/// <returns>A Task that represents the operation.</returns> | ||
public async Task RunAsync(CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
if(UserCodeInit.IsCallPreJit()) | ||
#if NET8_0_OR_GREATER | ||
AdjustMemorySettings(); | ||
#endif | ||
|
||
if (UserCodeInit.IsCallPreJit()) | ||
{ | ||
this._logger.LogInformation("PreJit: CultureInfo"); | ||
UserCodeInit.LoadStringCultureInfo(); | ||
|
@@ -248,6 +252,45 @@ private void WriteUnhandledExceptionToLog(Exception exception) | |
Console.Error.WriteLine(exception); | ||
} | ||
|
||
#if NET8_0_OR_GREATER | ||
/// <summary> | ||
/// The .NET runtime does not recognize the memory limits placed by Lambda via Lambda's cgroups. This method is run during startup to inform the | ||
/// .NET runtime the max memory configured for Lambda function. The max memory can be determined using the AWS_LAMBDA_FUNCTION_MEMORY_SIZE environment variable | ||
/// which has the memory in MB. | ||
/// | ||
/// For additional context on setting the heap size refer to this GitHub issue: | ||
/// https://github.com/dotnet/runtime/issues/70601 | ||
/// </summary> | ||
private void AdjustMemorySettings() | ||
{ | ||
try | ||
{ | ||
int lambdaMemoryInMb; | ||
if (!int.TryParse(Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_MEMORY_SIZE"), out lambdaMemoryInMb)) | ||
return; | ||
|
||
ulong memoryInBytes = (ulong)lambdaMemoryInMb * 1048576; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: break 1,048,576 down into 1024 x 1024 just for readability There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
// If the user has already configured the hard heap limit to something lower then is available | ||
// then make no adjustments to honor the user's setting. | ||
if ((ulong)GC.GetGCMemoryInfo().TotalAvailableMemoryBytes < memoryInBytes) | ||
return; | ||
|
||
AppContext.SetData("GCHeapHardLimit", memoryInBytes); | ||
|
||
// The RefreshMemoryLimit API is currently marked as a preview feature. Disable the warning for now but this | ||
// feature can not be merged till the API is no longer marked as preview. | ||
#pragma warning disable CA2252 | ||
GC.RefreshMemoryLimit(); | ||
#pragma warning disable CA2252 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PR dotnet/runtime#91622 was merged. Could you please remove the part around disabling the warning? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
catch(Exception ex) | ||
{ | ||
_logger.LogError(ex, "Failed to communicate to the .NET runtime the amount of memory configured for the Lambda function via the AWS_LAMBDA_FUNCTION_MEMORY_SIZE environment variable."); | ||
} | ||
} | ||
#endif | ||
|
||
#region IDisposable Support | ||
private bool disposedValue = false; // To detect redundant calls | ||
|
||
|
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.
nit: add the environment variable as a constant
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.
Done