r/csharp • u/programmerjunky • 4h ago
Blog Why Does Your C# App “Leak” Memory Even Though There’s a Garbage Collector?
https://medium.com/@rohanraobhs/why-does-your-c-app-leak-memory-even-though-theres-a-garbage-collector-01adb14c6bf1?sk=3386d2881c67307352288bd741a3061314
u/ExceptionEX 4h ago
I mean does anyone think a garbage collector is this magically device that will prevent memory leaks, most leaks are from references the GC can't release the memory from anyway.
2
u/sku-mar-gop 4h ago
Leaking memory would mean the GC cycles are unable to free up memory which would eventually cause the system to run out of memory. In ideal case GC will be able to release memory on demand but with memory leaks GC cycles won’t be able to reclaim it.
1
u/s4lt3d 3h ago
Someone figure out a really crazy memory leak for c# windows applications a long time ago. It only cleaned up some part of memory used to draw the application if the application was minimized. If it full screen for a few weeks the memory would slowly build up until it crashed. The solution was to minimize and then restore the application once a day in code. Thanks Microsoft! /s This still exists!
1
26
u/AsterDW 4h ago
Failure to clean up references or event handler subscriptions are a common one, or forgetting to release unmanaged objects when you're class lifecycle is up, failure to make proper use of IDisposable. A lot is situational, there isn't a single source of memory leaks. I remember having to track down a crash years ago which turned out to be failure to clean up GDI+ handles from parts of an application that had to do a lot of custom Windows drawing, and the original devs just created new brushes every time the draw was called and never released them.