GC.AddMemoryPressure
API usage example
#93717
-
When should we consider using the
https://learn.microsoft.com/en-us/dotnet/api/system.gc.addmemorypressure From the document, it's for the unmanaged memory increase, and is there any way for the managed memory pressure? For example, could I trigger GC when there's over 100MB managed memory allocated |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
If you're writing an application and you really want to kick off a GC run, just call If you're writing a library or application that interfaces with native memory, use that API. If you're deploying (or possibly writing) an application, and you want to make the GC possibly more aggressive, consider setting configuration in the runtime config. If you're deploying in a docker container, setting the limits on the container definition is also an option. If you're writing a library, do neither, but you might consider informing users about memory behavior. |
Beta Was this translation helpful? Give feedback.
If you're writing an application and you really want to kick off a GC run, just call
GC.Collect()
. That said, it's rare you should need to manually trigger a GC run.If you're writing a library or application that interfaces with native memory, use that API.
If you're deploying (or possibly writing) an application, and you want to make the GC possibly more aggressive, consider setting configuration in the runtime config. If you're deploying in a docker container, setting the limits on the container definition is also an option. If you're writing a library, do neither, but you might consider informing users about memory behavior.