Long Windows service with memory leak? Or just looks like one?

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

Recently, I was assigned an investigation into the reasons why the Windows service in production consumed about one drum (the server it runs on has 8 gigs). This is outside my experience as a developer, but for me there is a very good chance to find out how the garbage collector works in msdn and other sources. But at the moment I am very confused / when / the collection really works, for this reason, the article Iโ€™ve ever read is unclear.

1) I found a specific operation that increases the memory by 30 kb each time it is executed. 2) I went through the code very carefully and I think that I close everything correctly and delete links 3) I used several memory profiles, all of them seem to indicate that my old objects are related to gc. 4) If I leave the process completely inactive for several days, memory usage will suddenly drop to ~ 8 megabytes

Therefore, based on this, I am not even sure that I have a memory leak. Given that the GC is an expensive process, is it possible that I grew up to 1 gigabyte in production just because there was still a free ram and its purchase was โ€œcheaperโ€ than launching the GC? Especially considering that this service is launched ~ 6 times per second? If so, what options do I have? I understand that I canโ€™t force GC to start, do I have some kind of resort?

Thanks for any input you have, I understand that csharp's memory and gc leaks are a deep topic, and if it would be especially useful to read this topic, I would also be happy to point this way.

+6
source share
2 answers

Of course, you can force garbage collection - just call GC.Collect. This does not mean that you cannot, but the garbage collector is usually better versed when it needs to run than you. But here you can call it explicitly - as a debugging tool - to find out if the allocated memory is suitable for collection.

+3
source

A memory leak usually implies that the memory is never freed, and the process eventually crashes with an OutOfMemoryException . You say that after some time he is freed

4) If I leave the process completely inactive for several days, memory usage will suddenly drop to ~ 8 megabytes

You can of course force collection of garbage using GC.Collect . But, as others have said, this is not a very good long-term solution. I highly recommend that you familiarize yourself with garbage collection in this book . If you are still sure that you have a memory leak, you can dump the process in the production environment using Process Explorer . And analyze it later using WinDbg . If you cannot use dotTrace or ANTS for production, it would be much easier.

+2
source

Source: https://habr.com/ru/post/895608/


All Articles