Search code examples
c#.netasp.net-core.net-core

How can I diagnose a .NET process consuming high CPU?


I have an ASP.NET Core process running in Kubernetes. Suddenly something went wrong and CPU usage jumped from 8% to 100% and settled at that level. Memory usage did not change, so it looks like an infinite loop in a thread.

What instruments can I use to diagnose what is happening in the process?

What should I do to be able to diagnose such problems in the future?


Solution

  • .NET Core supports some commands that let us diagnose a CPU high or memory problem.

    First, install tools:

    dotnet tool install --global dotnet-trace
    dotnet tool install --global dotnet-counters
    

    Then we can use dotnet-trace ps to get the process ID that we want to trace.

    Then use the dotnet-counters command, so we can see the process using resource situations.

    dotnet-counters monitor -p 22884 --refresh-interval 1
    
    • -p: which process ID you want to trace
    • --refresh-interval: How often to refresh the monitor screen from commandline window (unit is second)

    if you want to only focus on cpu usage you can try to add this parameter --counters System.Runtime[cpu-usage]

    when we use the above command we will get a lot of Runtime information that can help us to diagnose from your process.

    [System.Runtime]
    % Time in GC since last GC (%)
    Allocation Rate (B / 1 sec)
    CPU Usage (%)
    Exception Count (Count / 1 sec)
    GC Committed Bytes (MB)
    GC Fragmentation (%)
    GC Heap Size (MB)
    Gen 0 GC Count (Count / 1 sec)
    Gen 0 Size (B)
    Gen 1 GC Count (Count / 1 sec)
    Gen 1 Size (B)
    Gen 2 GC Count (Count / 1 sec)
    Gen 2 Size (B)
    IL Bytes Jitted (B)
    LOH Size (B)
    Monitor Lock Contention Count (Count / 1 sec)
    Number of Active Timers
    Number of Assemblies Loaded
    Number of Methods Jitted
    POH (Pinned Object Heap) Size (B)
    ThreadPool Completed Work Item Count (Count / 1 sec)
    ThreadPool Queue Length
    ThreadPool Thread Count
    Time spent in JIT (ms / 1 sec)
    Working Set (MB)
    

    For more details, you can see Debug high CPU usage in .NET Core & dotnet-counters