Hi Sanping,
I guess your current question is mainly "why more memory increased and not released immediately in an ASP.NET Core Web API project". You can also see the memory also increased fast when using other ORMs to retrieve a lost of data in a Web API project. If not, please provide your code sample here.
The .NET Garbage Collector has two different modes (https://docs.microsoft.com/en-us/aspnet/core/performance/memory?view=aspnetcore-3.1#workstation-gc-vs-server-gc):
- Workstation GC: Optimized for the desktop. 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.
- Server GC. The default GC for ASP.NET Core apps. Optimized for the server. 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.
The GC mode can be set explicitly in the project file of the .NET DataStore sales demo. You can set ServerGarbageCollection to Workstation GC mode in the project file to see whether it works like what you expected:
<PropertyGroup>
<ServerGarbageCollection>false</ServerGarbageCollection>
</PropertyGroup>
Start this .NET DataStore sales demo again, you will see that process will only cost about 100~150 MB memory. It can at least prove that there is no memory leak in this demo.
Please note that Workstation GC mode is usually not recommended when running this application on the server. On a typical web server environment, CPU usage is more important than memory, therefore the Server GC mode is better. If memory utilization is high and CPU usage is relatively low, the Workstation GC might be more performant. For example, high density hosting several web apps where memory is scarce.
Regards, Logan