Skip to content

GC overestimates available memory #82815

Closed

Description

@edwinloubser

Description

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

Regression?

Possibly

Data

ECS Task Definition

Cpu: 256
Memory: 1024

GC.GetGCMemoryInfo

Compacted:False
Concurrent:False
TotalAvailable:3862mb
HighMemoryLoadThreshold:3476mb

Analysis

The runtime/GC detects the available memory incorrectly.

Activity

  1. added
    tenet-performance
    on Mar 1, 2023
  2. ghost added
    untriaged
    on Mar 1, 2023
  3. mangod9 commented on Mar 1, 2023

    @mangod9
    Member

    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!

  4. removed
    untriaged
    on Mar 1, 2023
  5. added this to the 8.0.0 milestone on Mar 1, 2023
  6. edwinloubser commented on Mar 1, 2023

    @edwinloubser
    Author

    @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:

    internal static MemoryPressure GetMemoryPressure()

  7. edwinloubser commented on Mar 1, 2023

    @edwinloubser
    Author

    @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

  8. jkotas commented on Mar 1, 2023

    @jkotas
    Member
  9. mangod9 commented on Mar 1, 2023

    @mangod9
    Member

    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.

  10. normj commented on Mar 1, 2023

    @normj

    I'm attempting to recreate the issue from the AWS side.

  11. Maoni0 commented on Mar 1, 2023

    @Maoni0
    Member

    ECS Task Definition
    Memory: 1024

    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?

  12. normj commented on Mar 1, 2023

    @normj

    @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.

  13. Maoni0 commented on Mar 1, 2023

    @Maoni0
    Member

    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 -

    1. 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.

    2. 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.

  14. edwinloubser commented on Mar 2, 2023

    @edwinloubser
    Author

    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

    image

  15. normj commented on Mar 2, 2023

    @normj

    @edwinloubser Yes that is the setting I am referring to.

  16. edwinloubser commented on Mar 2, 2023

    @edwinloubser
    Author

    @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.

    image

    @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?

  17. mangod9 commented on Mar 2, 2023

    @mangod9
    Member

    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.

  18. Maoni0 commented on Mar 2, 2023

    @Maoni0
    Member

    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.

  19. normj commented on Mar 2, 2023

    @normj

    @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.

  20. Maoni0 commented on Mar 2, 2023

    @Maoni0
    Member

    @normj absolutely. please share with me when you have the draft ready (my email alias at MS is maonis).

  21. edwinloubser commented on Mar 6, 2023

    @edwinloubser
    Author

    @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.

  22. Maoni0 commented on Mar 6, 2023

    @Maoni0
    Member

    @edwinloubser that's great to hear :)

  23. normj commented on Mar 20, 2023

    @normj
  24. ghost locked as resolved and limited conversation to collaborators on Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-GC-coreclrtenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions