-
Notifications
You must be signed in to change notification settings - Fork 478
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
Conversation
…process to match the max memory configured for the Lambda function.
PR on .NET runtime repository to remove the preview attributes for the new API: dotnet/runtime#91622 |
try | ||
{ | ||
int lambdaMemoryInMb; | ||
if (!int.TryParse(Environment.GetEnvironmentVariable("AWS_LAMBDA_FUNCTION_MEMORY_SIZE"), out lambdaMemoryInMb)) |
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
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done
// 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done
The .NET runtime does recognize the memory limits that have been set by Lambda's cgroup configuration. As part of .NET 8 the .NET team added support for configuring the max heap memory size via the
AppContext.SetData("GCHeapHardLimit", memoryInBytes);
andGC.RefreshMemoryLimit();
APIs. Refer to this GitHub issue for more information on these APIs. dotnet/runtime#70601The Lambda runtime client (Amazon.Lambda.RuntimeSupport) is now using these APIs for functions that target .NET 8 by taking the memory size configured for the Lambda function and communicating that size to the .NET runtime via the new APIs. This is done via the new
AdjustMemorySettings
method inLambdaBootstrap
inside RuntimeSupport. The method is called once before any Lambda invocations are made.If the users set the memory max memory settings themselves, for example using the
DOTNET_GCHeapHardLimit
, to a value less than what Lambda function is configured for then theAdjustMemorySettings
will return making no adjustments. Users might now they will run other processes in the background that will take away some of the memory and so tell .NET it has to use less then the full function's memory setting.New integration tests were added to confirm the memory settings. The tests only work for .NET 8. If the test run for .NET 6 they will with the Lambda function thinking it has more memory then it really does have. These were the first .NET 8 integration tests added for RuntimeSupport and the integration tests need to be reworked a bit to support both .NET 6 and .NET 8 integration tests.