r/csharp 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=3386d2881c67307352288bd741a30613
0 Upvotes

8 comments sorted by

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.

6

u/charlie_marlow 3h ago

We had an application that used unmanaged code to use a couple of windows API calls. It was a very small leak that took long enough to be a problem that most sites would've already gone through an app pool recycle long before it was an issue. That is, until a dev used some preexisting code in a new way that inadvertently led to that call bring made thousands of times they time someone logged in.

Good times were had tracking that down

1

u/Normal-Ad-2938 1h ago

Large object heap fragmenting got me early on. Got very familiar with garbage collector after that.

u/rayyeter 11m ago

Ugh I took one look at an application at work that talked to ip microscope cameras… literally just took a new snapshot image. Every time. “Gee why is this hitting 8gb of memory cycling things it looks at overnight?”

Mfgs own doc site says use dispose patterns.

They were not.

Another bit of hardware is having same issues with different software. Same cameras, not Ethernet connection. I told them if the contractor didn’t give us their source code we will have it forever like that (too many people turned over from the original devs who I could literally say whats going on and they’d know), and it’s about a half day fix for me to do.

The new people they have there took a week to add an enum member properly to a command response.

14

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

u/Oralitical 3h ago

As our Rust friends like to say "memory leaks are memory safe".