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.

Post History

80%
+6 −0
Q&A 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...

2 answers  ·  posted 2y ago by Alexei‭  ·  edited 2y ago by Alexei‭

#3: Post edited by user avatar Alexei‭ · 2022-01-10T06:10:27Z (over 2 years ago)
clarified the purpose of my question
  • 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](https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/large-object-heap) 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](https://stackoverflow.com/questions/118633/whats-so-wrong-about-using-gc-collect)
  • - **Runtime configuration options for garbage collection** - I have checked [GC settings](https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector) 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
  • 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](https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/large-object-heap) 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](https://stackoverflow.com/questions/118633/whats-so-wrong-about-using-gc-collect)
  • - **Runtime configuration options for garbage collection** - I have checked [GC settings](https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector) 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.
#2: Post edited by user avatar Canina‭ · 2022-01-06T17:43:33Z (over 2 years ago)
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] (https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/large-object-heap) 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](https://stackoverflow.com/questions/118633/whats-so-wrong-about-using-gc-collect)
  • - **Runtime configuration options for garbage collection** - I have checked [GC settings](https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector) 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
  • 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](https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/large-object-heap) 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](https://stackoverflow.com/questions/118633/whats-so-wrong-about-using-gc-collect)
  • - **Runtime configuration options for garbage collection** - I have checked [GC settings](https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector) 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
#1: Initial revision by user avatar Alexei‭ · 2022-01-06T14:47:36Z (over 2 years ago)
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] (https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/large-object-heap) 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](https://stackoverflow.com/questions/118633/whats-so-wrong-about-using-gc-collect) 
- **Runtime configuration options for garbage collection** - I have checked [GC settings](https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector) 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