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?
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.
1 comment thread