C#C
C#12mo ago
33 replies
TldrOlli

✅ Process API: Memory Leaks in Windows (?)

Hi all,

I'm developing an Asp.Net solution that allows registering CLI commands which are executed periodically. (for reference: GitHub)

When I'm running the service (as a windows service) with relatively few CLI commands to execute (every 5 seconds) I am experiencing higher and higher memory consumption on the host, to a point where memory+swap (16/32 Gig) are consumed and the PC becomes unusable.

That is, when I have two commands running, one that periodically checks if a winsvc is running (sc.exe query) and one that pings google (for more info, refer to the configuration here).

I tried both using Process (Namespace: System.Diagnostics) and the NuGet Package CliWrap, but the memory leak occurs with both.

Interestingly the opentelemetry reported memory consumption of the service - screenshot attached - remains relatively stable, checking in the task manager directly these numbers line up. The task manager itself cannot account for over half of the memory when the PC is at 100% memory usage.

Some points to note:
- The performance (execution duration) of the individual CLI commands begins to degrade quite fast, getting worse and worse the longer the service runs
- Restarting the windows service (not rebooting the machine) does not free any memory

Questions:
- Is this a known problem of the Process class? I have tried nearly all combinations of sc.exe, tasklist, ping - all run into memory leaks, so I would be somewhat confident to say it is somewhere on C# level (?)
- Can someone give me any pointers how to diagnose where the memory is going? If my Grafana Dashboard (which aligns with the Task Manager) is right, the application itself is consuming relatively few memory (big questionmark here!)

I'd be happy about any kind of advice, cheers and thanks in advance
Olli
image.png
Was this page helpful?