Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long running windows service with a memory leak? Or just looks like one?

This sounds a little odd, but I'm not sure if my process has a memory leak or not, and I was hoping to get some information.

I was recently assigned an investigation into why a windows service in production was consuming roughly a gig of ram (the server it is running on has 8 gigs). This is outside of my experience as a developer, but it's been a very good chance for me to read up on how garbage collection is working on msdn and other sources. But at this point I'm very confused about /when/ collection actually runs, on this point, ever article I read is vague.

1) I have found a specific operation that increases memory by ~30kb each time it is executed. 2) I have very carefully gone over the code and believe that I am properly closing everything, and removing references 3) I have used several memory profilers, all of them seem to indicate that my old objects are linked to the gc. 4) If I leave the process absolutely idle for a few days, the memory usage suddenly plummets down to ~8 megs

So based on this, I'm not even sure I have a memory leak. Given that GC is an expensive process, is it possible that I grew to 1 gig in production just because there was still free ram to be had, and acquiring it was "cheaper" than running GC? Especially given that this service is run ~6 times a second? If this is the case, what options do I have? I am of the understanding that I cannot forceably trigger GC, do I have any resort?

Thank you for any input you might have, I realize memory leaks and gc in csharp is a deep topic and if there's a particularly helpful read on the subject, I'd be happy to be pointed that way as well.

like image 217
Dio Avatar asked Aug 22 '11 16:08

Dio


1 Answers

You certainly CAN force a garbage collection - just call GC.Collect. It's not that you can't but that the garbage collector usually does a better job of figuring out when it should run than you do. But here, you can call it explicitly - as a debugging tool - to see whether or not the allocated memory is eligible for collection.

like image 84
500 - Internal Server Error Avatar answered Sep 20 '22 01:09

500 - Internal Server Error