Memory is not releasing in dotnet6

usha 100 Reputation points
2023-12-05T14:48:57.93+00:00

I'm currently working on a .NET 6 Web API project, and I'm facing an issue with memory not being released properly.

I have thoroughly analyzed the code and ensured that I'm not creating any memory leaks or holding onto unnecessary resources. However, the memory consumption continues to rise, even during idle periods.

I have tried the following steps to address the issue:

Created a new blank .NET 6 Project , but the same , Memory is not releasing

Is there anything specific to .NET 6 or Web API that I should be aware of regarding memory management? What could be causing the memory not to release properly?

Any help or insights would be greatly appreciated. Thank you!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
721 questions
{count} votes

Accepted answer
  1. Wenbin Geng 716 Reputation points Microsoft Vendor
    2023-12-06T07:42:31.3066667+00:00

    Hi @usha, Welcome to Microsoft Q&A.

    The .NET garbage collector has two different modes:

    Workstation GC: Optimized for desktops.

    Server GC: The default GC for ASP.NET Core applications. Optimized for servers.

    GC mode can be set explicitly in the project file or in the runtimeconfig.template.json file of the published application. The following markup shows setting ServerGarbageCollection in the project file:

    <PropertyGroup>
       <ServerGarbageCollection>true</ServerGarbageCollection>
    </PropertyGroup>
    

    Changing the ServerGarbageCollection in the project file requires rebuilding the app.

    Note: Server garbage collection is not available on machines with a single core. For more information, see IsServerGC.

    The test results of the memory profile of workstation GC and server GC occupying the same large amount of RPS are as follows:

    1. Working set reduced from 500 MB to 70 MB.

    2.GC generates 0 times per second (instead of recycling every two seconds).

    3.GC dropped from 300 MB to 10 MB.

    After disabling the GC in Server mode through

    <ServerGarbageCollection>false</ServerGarbageCollection>
    

    , the GC becomes Workstation mode, and the program will recycle memory more aggressively. Of course, after changing the server-side program to Workstation mode, the performance of the program will be affected, so unless there is a good reason, it is not recommended. After all, for the server, idle memory is a waste.

    In summary, whether to use

    <ServerGarbageCollection>false</ServerGarbageCollection>
    

    depends on your specific production environment. In a typical web server environment, CPU usage is more important than memory, so server GC is better. If memory usage is high and CPU usage is relatively low, a workstation GC may provide higher performance. For example, high density hosting of several web applications with insufficient memory.

    Best Regards,

    Wenbin


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful