受欢迎的博客标签

ASP.Net core actual record

Published

The Garbage Collector in the CLR is a very complicated, configurable and self-tuning creature that may change behavior based on application needs.

 

CLR memory 

Each process has its own, separate virtual address space. All processes on the same computer share the same physical memory and the page file, if there is one.
By default, on 32-bit computers, each process has a 2-GB user-mode virtual address space.

In the common language runtime (CLR), the garbage collector (GC) serves as an automatic memory manager. The garbage collector manages the allocation and release of memory for an application. For developers working with managed code, this means that you don't have to write code to perform memory management tasks. Automatic memory management can eliminate common problems, such as forgetting to free an object and causing a memory leak or attempting to access memory for an object that's already been freed.

Memory release

The garbage collector's optimizing engine determines the best time to perform a collection based on the allocations being made. When the garbage collector performs a collection, it releases the memory for objects that are no longer being used by the application. It determines which objects are no longer being used by examining the application's roots. An application's roots include static fields, local variables and parameters on a thread's stack, and CPU registers. 

Conditions for a garbage collection

Garbage collection occurs when one of the following conditions is true:

1.The system has low physical memory. This is detected by either the low memory notification from the OS or low memory as indicated by the host.
2.The memory that's used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.
3.The GC.Collect method is called. In almost all cases, you don't have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

 

To satisfy different memory usage requirements the GC has some options to configure how it operates. 

There are two main modes:

1.Workstation mode (designed to minimize delays) 

2.Server mode (designed for maximum application throughput).

Default the application uses Workstation garbage collection

Configures  the application uses server garbage collection(Server GC)

<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection> = Server GC

Server GC is optimized for application throughput in favor of longer GC pauses. Memory consumption will be higher, but application can process greater volume of data without triggering garbage collection.

 

Configures  the application uses workstation garbage collection (Workstation GC)

<ConcurrentGarbageCollection>false</ConcurrentGarbageCollection> = Workstation GC

Workstation GC is designed for desktop applications to minimize the time spent in GC. In this case GC will happen more frequently but with shorter pauses in application threads.

 

settings that can be changed at run time

see:https://docs.microsoft.com/en-us/dotnet/core/run-time-config/garbage-collector

runtimeconfig.json

{
   "runtimeOptions": {
      "configProperties": {
         "System.GC.Server": true
      }
   }
}

1.服务器内存回收配置 Nop.Web.csproj

<PropertyGroup>

<ServerGarbageCollection>false</ServerGarbageCollection>

</PropertyGroup>.

 

 

Understanding different GC modes with Concurrency Visualizer

Official Documentation:https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/fundamentals?redirectedfrom=MSDN