.Net Rising memory problem

I have an application that does a bunch of parsing on text. After each pass, it spits out some information into the database and cleans up all internal state.

My problem is the memory allocated in Windows Task Mgr / Resource Monitor keeps growing and growing. I made some profile using .Net Mem Profiler and it looks like it should be omitted. Here's a screenshot from the profiler:

enter image description here

But in Task Mgr, after each pass, the amount of personal working set of memory increases. I would like the memory to grow as it is used and then return to normal after each pass, so I can keep this working.

Any tips on what to look for or what ideas is causing this?


source to share

5 answers

Task Mgr is not an exact representation of the memory used by your application. Its a more granular view of how many windows of memory have been allocated or scheduled for your application - if you need the application more, windows can expand that number, but if your application needs less, windows may not reallocate that memory until need another application ..

I think what you have is a pretty accurate representation of what your application is actually doing (i.e. not leaking it).



A few things to check:

  • For event handlers that keep objects alive, make sure that if the object that is subscribing to the event goes out of scope before the object publishes the event that it unsubscribes from the event, to prevent the object from being published, reference it.
  • Make sure you call dispose on any object that implements IDisposable. In general, objects that implement IDisposable contain resources that need extra care.
  • If you are referencing any com objects, make sure you release them correctly.

You shouldn't call GC.Collect()

in production code.



This forces .NET to collect all unused objects from memory, so fixing some of them:





If you have a memory leak, you can use the SOS Debug Extension to try and find it. This article is also a very good example and is slightly more complete than my answer.

You can use this in VS or WinDbg and the only difference is how you load the dll. For Visual Studio, first enable unmanaged debugging in the debug tab of the project properties. When it's time to download it, use .load SOS.dll

in Immediate Window

. For WinDbg, either open the executable or attach it to a process and use .loadby sos clr

for .NET 4 or .loadby sos mscorwks

2 or 3.5 to load it .

After the app has started for a while, suspend it (cancel everything). You can now download SOS. After success, enter !dumpheap -stat

. This will indicate how much memory each class is using. If that's not enough to find a leak, another article I linked goes deeper into how to find a memory leak.



One way to solve this problem is to split your work into separate executable programs. As soon as the program finishes its work and finishes, all its memory will be restored by the system.



All Articles