Tracking vs No-Tracking – Impact on Performance and Memory

EF Core’s default change tracking is a double-edged sword: it enables automatic updates but consumes memory and CPU. Choosing AsNoTracking() wisely can cut GC pressure by 90 % on read-heavy endpoints without losing correctness.

Real-Life Analogy: Security Cameras vs Click Counters

A museum can film every visitor with CCTV (tracking) or just click a counter at the door (no-tracking). Cameras capture rich behaviour but store gigabytes; clickers are cheap but blind to theft. In code: track entities you plan to update, click-count the rest.

How Tracking Works

  1. Identity Map – dictionary keyed by primary-key ➜ keeps single CLR instance per row.
  2. Snapshot Storage – original values captured after materialisation or AcceptAllChanges().
  3. DetectChanges() – walks all tracked entities comparing current vs snapshot.
  4. Change Graph – generates Insert/Update/Delete commands.

Memory Footprint Benchmark

// 100 k Order rows, .NET 8, SQL Server 2022, Release build
Query                                   Gen0 KB      Time (ms)
---------------------------------------------------------------
Tracked ToListAsync()                  480 000           1900
AsNoTracking().ToListAsync()            60 000           1400
AsNoTrackingWithIdentityResolution()   110 000           1500

Decision Matrix

ScenarioTracking?Notes
Read-only API listNoReturn DTOs, not entities.
Edit Form (load + update)YesAttach automatically, simplifies PATCH.
Background exportNoStream to CSV.
Graph update with Attach()YesChange tracker resolves reference graph.

Advanced: Clearing & Splitting Contexts

  • context.ChangeTracker.Clear() – drop all tracked entities mid-request.
  • CQRS Pattern – query stack uses no-tracking, command stack uses tracking.
  • Scoped DbContext per operation – cheapest way to isolate.

Identity Resolution Without Tracking

AsNoTrackingWithIdentityResolution() gives deduplicated references (good for JSON loops) without snapshots—memory halfway between the two modes.

Final Thoughts

Tracking is like a high-def CCTV system: switch it on when you need evidence, turn it off when you’re just counting visitors. Declare intent explicitly—your GC and latency graphs will smile back.

Tracking vs No-Tracking – Impact on Performance and Memory | SimplyAdvanced.dev