Profiling a C# app in VS Code with SharpLsp: CPU heat on hot lines, the hottest functions, and a leaking static dictionary found and fixed
Transcript
This is Storefront, a C# app that's running right now. SharpLsp attaches to it, no restart needed, and samples where its time goes.
Stop the profiler, and every hot line lights up in the editor: the catalog parse, the discount loop, and the invoice built one string at a time.
The results list the hottest functions and the hottest lines, so you know exactly where to start.
Now track memory on the same app. SharpLsp snapshots the heap every ten seconds.
The suspects rank by consistent growth, and CustomerSession grows in every snapshot, along with the byte arrays it holds. Click it, and you jump to its code.
So, what's keeping those sessions alive?
A static dictionary, named in the graph, that holds every session the app has ever seen.
The fix: keep the last thousand orders, and evict the rest.
Rebuild, run it again, and track memory.
This time the heap holds steady. That's the SharpLsp profiler, for C# and F#. Free, and open source.