You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Containers running on AWS Fargate runs out of memory, more often since upgrading to .Net 7. It seems like the Regions based GC is less aggressive? And this is possibly not playing well, with what seems to be the root cause which is that the GC is not configured correctly.
Configuration
TargetFrameworkVersion: .NET 7.0.3
OSArchitecture: X64
OSDescription: Linux 5.10.162-141.675.amzn2.x86_64 #1 SMP Mon Jan 9 22:45:11 UTC 2023
Thanks @edwinloubser for reporting the issue. Could you please try with libclrgc.so if that improves things for you. If you can share some dumps/traces around when the OOMs happen that would be helpful. We have fixed a bug with some spurious OOMs in 7.0.3, so please ensure you are running with that build. Thx!
@mangod9 I won't be able to provide dumps or traces. I have downgraded to .Net 6 and will see how things go. But there is clearly issue with the GC configuration, no? For example, it will never detect when there is memory pressure. The following library code won't ever work:
@mangod9 I think there is a slight misunderstanding, we are not getting OOM's from the runtime. The task is getting killed by AWS Fargate due to the task exceeding its defined memory budget. My analysis is that I think the unusual memory growth of the .Net process is due to the GC thinking it has more memory than it actually has. In this example the task only has 1024mb memory but the GC thinks TotalAvailable is 3862mb
Do ascertain whether its the memory limit issue you could try to set DOTNET_GCHeapHardLimit to 1024mb so that the GC will keep allocations under that limit.
I'm not sure what this ECS definition translates to. if this translate to the soft limit, ie memory.soft_limit_in_bytes or memory.low then our OS layer does not read that (GC just gets that value from our OS layer). we only read the hard limit (memory.limit_in_bytes/memory.max). this is something we were aware of and will be addressing. but this is what we did in 6.0 too. so as far as reading the limit goes, there's no changes there AFAIK. if you call GetGCMemoryInfo in 6.0, does it reports a different value for TotalAvailable?
@Maoni0 That is interesting. I have both a .NET 6 and .NET 7 app deployed to ECS and both of them are returning back 4MB despite the ECS task memory being set to 1MB.
In Fargate the task memory is not 1 to 1 for the container's limits because a task can contain multiple containers. @edwinloubser can set the MemoryLimitMiB on the container configuration within the ECS task. I set MemoryLimitMiB to 1024 and then the GetGCMemoryInfo return back in my case 805,306,368.
But this doesn't answer the question why is there a difference between .NET 6 and 7.
here's what I think how we can approach this, let me know if you don't think this is reasonable -
we should make sure @edwinloubser's env is the same in 6.0 and 7.0 -
if the output from calling GetGCMemoryInfo is the same, and @edwinloubser is seeing a lower memory usage on 6.0, proceed to 2); if GetGCMemoryInfo shows different info on 6.0, then we should investigate why that is.
could @edwinloubser try out libclrgc.so which has the 6.0 GC. you can invoke it by setting the COMPlus_GCName to libclrgc.so. if that reverts to 6.0 behavior, clearly it's a problem with the default 7.0 GC so the GC team should look at that. however, for us to investigate, we will need some form of perf data. there's not much we can do with no data at all.
if it still shows the same increased memory behavior, it means something else in the new version of .NET has changed because GC is driven by app behavior. again we'll need some form of data to do our investigation.
Apologies, been on a flight and only able to respond now. Running the app under 6.0 has resulted from what I can tell in much better behavior. Which correlates with what we have seen in some of our other microservices, we had to rollback after the upgrade to 7.0. Too many were starting to get killed by running out of memory.
@Maoni0 I have reverted to 7.0 and tried setting the GCName but failed with two different approaches. Error in logs is "Failed to create CoreCLR, HRESULT: 0x80004005"
First attempt was by setting an environment variable defined in dockerfile after that failed, I tried to use runtimeconfig but that also results in the same error as above. Any suggestions?
@normj thanks for investigation that helps a lot. Just to confirm the setting you are referring to is
@normj thank you, we tested it out on some problematic services, and it has made a profound difference. One could argue that memory is now being underutilized.
@Maoni0 with the container now being correctly memory limited, the GC does report the correct available memory and works perfectly. This is an adequate resolution for me, the exact reasons why 6.0 performed better than 7.0 seems irrelevant now.
It might however be worthwhile updating some documentation somewhere to detail this?
Thanks for trying out the solution. Good to know the config change has made things much better. The difference between 6 and 7 could be just that the allocation patterns are now different and also that the new GC regions functionality is handling decommitting memory, based on the available memory heuristic.
It might however be worthwhile updating some documentation somewhere to detail this?
indeed. @edwinloubser can you please tell me how you were setting the memory limit before? did you set it in the Task size section instead of the container section? we should include this info in our container doc as AWS is an important platform for us obviously.
@Maoni0 I notice this issue is also affects .NET in AWS Lambda (serverless functions) where the GC thinks it has more memory then it really has. I think the behavior can be corrected using the DOTNET_GCHeapHardLimit environment variable. Strangely enough the runtime config setting System.GC.HeapHardLimit does not seem to have an impact but I'm still testing around.
I was thinking of writing a blog post for AWS explaining about the GC tweaks for containers and Lambda functions. Once I have a draft written I would love to share it with you to get your feedback if that is okay.
@Maoni0 yes, we only set the size of the Task and the Container settings were left as default. The fix was to marry up the two to be the same. The change has been amazing for us, we went from being flaky to rock solid :)
@normj it would be great if you can share the blog post, I would like to forward it on to the rest of the teams in our company.
Activity
mangod9 commentedon Mar 1, 2023
Thanks @edwinloubser for reporting the issue. Could you please try with libclrgc.so if that improves things for you. If you can share some dumps/traces around when the OOMs happen that would be helpful. We have fixed a bug with some spurious OOMs in 7.0.3, so please ensure you are running with that build. Thx!
edwinloubser commentedon Mar 1, 2023
@mangod9 I won't be able to provide dumps or traces. I have downgraded to .Net 6 and will see how things go. But there is clearly issue with the GC configuration, no? For example, it will never detect when there is memory pressure. The following library code won't ever work:
runtime/src/libraries/System.Private.CoreLib/src/System/Buffers/Utilities.cs
Line 38 in ab35b89
edwinloubser commentedon Mar 1, 2023
@mangod9 I think there is a slight misunderstanding, we are not getting OOM's from the runtime. The task is getting killed by AWS Fargate due to the task exceeding its defined memory budget. My analysis is that I think the unusual memory growth of the .Net process is due to the GC thinking it has more memory than it actually has. In this example the task only has 1024mb memory but the GC thinks TotalAvailable is 3862mb
jkotas commentedon Mar 1, 2023
cc @normj
mangod9 commentedon Mar 1, 2023
Do ascertain whether its the memory limit issue you could try to set
DOTNET_GCHeapHardLimitto 1024mb so that the GC will keep allocations under that limit.normj commentedon Mar 1, 2023
I'm attempting to recreate the issue from the AWS side.
Maoni0 commentedon Mar 1, 2023
I'm not sure what this ECS definition translates to. if this translate to the soft limit, ie memory.soft_limit_in_bytes or memory.low then our OS layer does not read that (GC just gets that value from our OS layer). we only read the hard limit (memory.limit_in_bytes/memory.max). this is something we were aware of and will be addressing. but this is what we did in 6.0 too. so as far as reading the limit goes, there's no changes there AFAIK. if you call
GetGCMemoryInfoin 6.0, does it reports a different value for TotalAvailable?normj commentedon Mar 1, 2023
@Maoni0 That is interesting. I have both a .NET 6 and .NET 7 app deployed to ECS and both of them are returning back 4MB despite the ECS task memory being set to 1MB.
In Fargate the task memory is not 1 to 1 for the container's limits because a task can contain multiple containers. @edwinloubser can set the
MemoryLimitMiBon the container configuration within the ECS task. I setMemoryLimitMiBto 1024 and then theGetGCMemoryInforeturn back in my case805,306,368.But this doesn't answer the question why is there a difference between .NET 6 and 7.
Maoni0 commentedon Mar 1, 2023
here's what I think how we can approach this, let me know if you don't think this is reasonable -
we should make sure @edwinloubser's env is the same in 6.0 and 7.0 -
if the output from calling GetGCMemoryInfo is the same, and @edwinloubser is seeing a lower memory usage on 6.0, proceed to 2); if GetGCMemoryInfo shows different info on 6.0, then we should investigate why that is.
could @edwinloubser try out libclrgc.so which has the 6.0 GC. you can invoke it by setting the COMPlus_GCName to libclrgc.so. if that reverts to 6.0 behavior, clearly it's a problem with the default 7.0 GC so the GC team should look at that. however, for us to investigate, we will need some form of perf data. there's not much we can do with no data at all.
if it still shows the same increased memory behavior, it means something else in the new version of .NET has changed because GC is driven by app behavior. again we'll need some form of data to do our investigation.
I explained this in this blog post.
edwinloubser commentedon Mar 2, 2023
Apologies, been on a flight and only able to respond now. Running the app under 6.0 has resulted from what I can tell in much better behavior. Which correlates with what we have seen in some of our other microservices, we had to rollback after the upgrade to 7.0. Too many were starting to get killed by running out of memory.
@Maoni0 I have reverted to 7.0 and tried setting the GCName but failed with two different approaches. Error in logs is "Failed to create CoreCLR, HRESULT: 0x80004005"
First attempt was by setting an environment variable defined in dockerfile after that failed, I tried to use runtimeconfig but that also results in the same error as above. Any suggestions?
@normj thanks for investigation that helps a lot. Just to confirm the setting you are referring to is
normj commentedon Mar 2, 2023
@edwinloubser Yes that is the setting I am referring to.
edwinloubser commentedon Mar 2, 2023
@normj thank you, we tested it out on some problematic services, and it has made a profound difference. One could argue that memory is now being underutilized.
@Maoni0 with the container now being correctly memory limited, the GC does report the correct available memory and works perfectly. This is an adequate resolution for me, the exact reasons why 6.0 performed better than 7.0 seems irrelevant now.
It might however be worthwhile updating some documentation somewhere to detail this?
mangod9 commentedon Mar 2, 2023
Thanks for trying out the solution. Good to know the config change has made things much better. The difference between 6 and 7 could be just that the allocation patterns are now different and also that the new GC regions functionality is handling decommitting memory, based on the available memory heuristic.
Maoni0 commentedon Mar 2, 2023
indeed. @edwinloubser can you please tell me how you were setting the memory limit before? did you set it in the Task size section instead of the container section? we should include this info in our container doc as AWS is an important platform for us obviously.
normj commentedon Mar 2, 2023
@Maoni0 I notice this issue is also affects .NET in AWS Lambda (serverless functions) where the GC thinks it has more memory then it really has. I think the behavior can be corrected using the
DOTNET_GCHeapHardLimitenvironment variable. Strangely enough the runtime config settingSystem.GC.HeapHardLimitdoes not seem to have an impact but I'm still testing around.I was thinking of writing a blog post for AWS explaining about the GC tweaks for containers and Lambda functions. Once I have a draft written I would love to share it with you to get your feedback if that is okay.
Maoni0 commentedon Mar 2, 2023
@normj absolutely. please share with me when you have the draft ready (my email alias at MS is maonis).
edwinloubser commentedon Mar 6, 2023
@Maoni0 yes, we only set the size of the Task and the Container settings were left as default. The fix was to marry up the two to be the same. The change has been amazing for us, we went from being flaky to rock solid :)
@normj it would be great if you can share the blog post, I would like to forward it on to the rest of the teams in our company.
Maoni0 commentedon Mar 6, 2023
@edwinloubser that's great to hear :)
normj commentedon Mar 20, 2023
@edwinloubser and others here is the blog post that goes deeper into this issue and what to do about it.
https://aws.amazon.com/blogs/developer/configuring-net-garbage-collection-for-amazon-ecs-and-aws-lambda/