Skip to content
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

Merged
merged 3 commits into from
Oct 12, 2023

Conversation

normj
Copy link
Member

@normj normj commented Sep 5, 2023

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); and GC.RefreshMemoryLimit(); APIs. Refer to this GitHub issue for more information on these APIs. dotnet/runtime#70601

The 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 in LambdaBootstrap 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 the AdjustMemorySettings 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.

…process to match the max memory configured for the Lambda function.
@normj
Copy link
Member Author

normj commented Sep 5, 2023

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))
Copy link
Contributor

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

Copy link
Member Author

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;
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines 281 to 285
// 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
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@normj normj changed the base branch from feature/net8 to dev October 12, 2023 15:32
@normj normj merged commit de1d04e into dev Oct 12, 2023
3 checks passed
@normj normj deleted the normj/adjust-memory branch October 12, 2023 15:39
@normj normj mentioned this pull request Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants