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

75%
+4 −0
Q&A Can I set a memory limit for the .NET Core garbage collector to collect objects more aggressively?

Generally speaking, the frequency of garbage collection is a space / time tradeoff: collection effort live object size GC overhead ~ ----------------- = ---------------- ...

posted 2y ago by meriton‭

Answer
#1: Initial revision by user avatar meriton‭ · 2022-01-10T00:04:48Z (over 2 years ago)
Generally speaking, the frequency of garbage collection is a space / time tradeoff:


                  collection effort   live object size
    GC overhead ~ ----------------- = ---------------- 
                     memory freed     dead object size 

In defaulting to collect garbage only when memory is starting to get scarce, the runtime maximizes dead object size, and thus the efficiency of collection. 

When determining whether memory is scarce, the runtime assumes that the Operating System's (or container's) free memory is available for use. In your case, this isn't the case, because you want to share that memory with many other applications. The typical response would be to tell the runtime how much memory it is allowed to use by setting `System.GC.HeapHardLimit` (or `System.GC.HeapHardLimitPercent`). It is worth noting that these settings set the maximum size of the heap, which is only part of the total memory used by your process. In particular, the just in time compiled executable code, as well as any native memory used, does not count towards that limit, so if you are aiming for 1GB total memory, you should be setting the heap limit quite a bit lower than that (as a rough guideline: if not specified, the max heap size [defaults to 75% of available memory](https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector#heap-limit-percent)).

For applications with very dynamic live object sizes, better collection efficiency may be achieved by manually triggering collection when live object size is particularly low (and dead object size is considerable). Whether this is true for your file upload depends on how significantly the file upload affects the overall live object size, and how easy it is to identify times where no file upload is running (which may be non-trivial due to concurrency). It may be easier to simply set a heap size limit instead.