Course contentsShow
← C#
Lesson 1751 of 1,90816. Performance and DiagnosticsFree lesson

Analyzing CPU Usage with DotTrace Timelines

Learners will correlate high CPU spikes with specific method execution paths.

In this lesson, you will learn how to pinpoint exact lines of code that cause CPU spikes by correlating high-load time segments with specific call stacks in dotTrace.

From Aggregate Time to Execution Flow

While traditional profiling might tell you that a method like ProcessData() consumes 40% of your total execution time, it doesn't always explain when or why that consumption happens. Is it a steady load, or is it spiking during a specific event, like a UI interaction or an API request? DotTrace Timeline profiling captures the chronological order of execution, allowing you to bridge the gap between "what" is running and "when" it hits the CPU.

Correlating Spikes with Call Paths

When you open a Timeline profile in dotTrace, you are presented with a horizontal thread view. High CPU activity is visualized as dense blocks of color. To correlate this:

  1. Select the region: Use your mouse to highlight a specific period of high CPU usage in the timeline chart.
  2. Filter the Call Tree: Once selected, the "Call Tree" view automatically filters to show only the methods active during that specific timeframe.

Imagine you have a DataProcessor class:

public void ProcessData(List<Item> items) 
{
    // High CPU path
    var results = items.Where(i => i.IsValid).OrderBy(i => i.Weight).ToList(); 
    UpdateUI(results);
}

If your profile shows a CPU spike exactly when ProcessData is triggered, the Timeline view will confirm if the OrderBy method is the culprit by showing a disproportionate block of time spent within that LINQ extension method compared to the rest of the application.

By zooming into these specific time slices, you ignore the "noise" of background threads or idle time and focus exclusively on the code paths currently starving your processor. If you see deep call stacks in the Timeline that align with your spikes, you can verify if you are performing redundant calculations or inefficient algorithm processing that should be cached or refactored.

Key Takeaway: Correlating high CPU spikes with Timeline snapshots allows you to isolate the specific method execution paths responsible for performance degradation, moving you beyond aggregate stats to precise, time-bound diagnosis.