Test for memory leaks in .NET applications

2020-06-04 03:27发布

问题:

Are there any good(and free) tools for either analyzing static source or running programs to help detect memory leaks?

I've built some windows services and want to make sure the are not going to consume memory if I leave them running for weeks on end.

回答1:

Doesn't meet your requirements of being free, but there's one by Red Gate I think is worth suggesting - ANTS Memory Profiler

I've used a number of their tools before (some on trial, some we've bought) and found them to be very good.



回答2:

I can recommend http://memprofiler.com/, but it is not free.



回答3:

Redgate Ants Performance Profiler might help. It isn't free... but there is a 14 day trial which may be enough to get your services working.



回答4:

Let your service run and use Performance Monitor to collect information about the memory usage of the service. Also, if you use any unmanaged resources make sure your code properly disposes of these resources using IDisposable. In an unmanaged environment you can easily have leaks by simply forgetting to release pointers, but that is not possible in a managed environment. However, if you keep allocating new objects and keep references to them so they cannot be garbage collected your service will require more and more memory.



回答5:

I've used Microsoft's CLR Profiler with some success. Make sure to read its documentation to get the full benefits.



回答6:

Profile Sharp is a free (open-source) performance and memory profiler for .NET



回答7:

Don't forget that you can use the Windows Task Manager to detect at least the presence of memory leaks in your service.

Another way to use the Task Manager to track objects it to switch to the Processes tab, then go to View > Select Columns and check USER Objects and GDI Objects (GDI probably not useful to you since you're running a Service, but others may find it useful.)