Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Can I set a memory limit for the .NET Core garbage collector to collect objects more aggressively?

Post

Can I set a memory limit for the .NET Core garbage collector to collect objects more aggressively?

+6
−0

I have an ASP.NET Core 5 web application that provides functionality that involves file upload + rather large processing (memory consumption mainly). After each processing (with success or not) the memory could be reclaimed.

I have a stress test the endpoint with large files and checked the w3wp.exe process Memory (it runs in IIS) which goes something like this:

  • step 1: 400M
  • step 2: 800M
  • step 3: 1200M
  • step 4: 1800M
  • step 5: 1300M

So there seems to be no memory leak, but GC kicks in very late. I would like for it to begin the cleanup faster, but I cannot seem to find a way to do this. What I have found/tried:

  • why the delay in collection? - objects larger than 85KB are considered large by the GC and will be collected less frequent than smaller objects
  • forcing the GC collection after each operation - this can be done using GC.Collect(), but it is not recommended to do so
  • Runtime configuration options for garbage collection - I have checked GC settings and applied a limit for System.GC.HeapHardLimit, the final configuration looking like this:
{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.AspNetCore.App",
      "version": "5.0.0"
    },
    "runtimeOptions": {
      "configProperties": {
        "System.GC.HeapHardLimit": 1000000000
      }
    },
    "configProperties": {
      "System.GC.Server": true,
      "System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
      "System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
    }
  }
}

The memory still goes beyond 1GB and stays like this until I process more data and it is finally reclaimed.

  • IIS application pool memory limit - setting the memory limit on the application pool will cause it to be recycled, not forcing the GC to act faster

The application runs on an internal shared server and I would like to have a reasonable peak memory for it.

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Why is that a problem? (4 comments)
Why is that a problem?
meriton‭ wrote over 2 years ago

Does this memory use cause an actual problem for you? Most modern servers comes with hundreds of GB of memory, so you should have memory to spare?

Alexei‭ wrote over 2 years ago

Unfortunately, the application I am developing now is running on servers that are shared by multiple .NET applications and the memory is not that generous (although its price is pretty low when compared to other software development costs).

meriton‭ wrote over 2 years ago

I see. Knowing that you are concerned about peak memory use, rather than, say, the length of garbage collector pauses helps writing targeted answers.

Alexei‭ wrote over 2 years ago

meriton‭ Yes, I have not realized the XY problem in my post. I have added a paragraph to clarify this.