What strategies and tools are useful for finding memory leaks in .NET?
I use Scitech's MemProfiler when I suspect a memory leak.
So far, I have found it to be very reliable and powerful. It has saved my bacon on at least one occasion.
The GC works very well in .NET IMO, but just like any other language or platform, if you write bad code, bad things happen.
Finding memory leaks in C# application
You need memory profiler to debug such problems. For example:
- CLR Profiler
- .Net Memory Validator
- GlowCode
- ANTS Memory Profiler
Also see suggestions from the other question about memory leaks.
Basically, this boils down to finding the objects in memory that stay here while they shouldn't. It can be event handler holding reference to its class or some collection of objects holding references to their parents, and so on. After finding the root cause you may need to restructure your application to get rid of the unnecessary references. This can be as simple as adding forgotten event unsubscription but in non trivial cases might require applying some structural design patterns. This part is very application specific.
memory leaks tool for c# .net
You can use the Ants Profiler Pro by Red Gate software
ANTS Performance Profiler is an application profiler for .NET desktop,
ASP.NET, and ASP.NET MVC applications:
- Find performance bottlenecks fast by profiling both the .NET code and the data access layer
- Get rich performance data, right-down to line-level timings and expensive database queries
- Save time going round in circles diagnosing and debugging – let the profiler do the hard work for you
- Explore unfamiliar code bases
Also check How to identify memory leaks in the common language runtime
Tools for Memory leaks in .Net executable
Redgate has a nice memory profiler you can download here. It even comes with a 14-day trial. I have used it and it is very good.
Can there be memory leaks in .NET? If yes, what are the best tool out there?
You can use a tool like CLR Profiler,VSTS Profiler, .NET Memory Profiler or CLR Profiler to check your object size etc, as they say you can Find Memory Leaks and Optimize Memory Usage in any .NET Program.
Strategies For Tracking Down Memory Leaks When You've Done Everything Wrong
One technique I would try is to systematically reduce the amount of code you need to demonstrate the problem without making the problem go away. This is informally known as "divide and conquer" and is a powerful debugging technique. Once you have a small example that demonstrates the same problem, it will be much easier for you to understand. Perhaps the memory problem will become clearer at that point.
Memory leaks in .NET
Block the finalizer thread. No other objects will be garbage collected until the finalizer thread is unblocked. Thus the amount of memory used will grow and grow.
Further reading: http://dotnetdebug.net/2005/06/22/blocked-finalizer-thread/
Related Topics
Ide's for C# Development on Linux
How to Get the Current Line Number
Accessing Password Protected Network Drives in Windows in C#
How to Get a Path to the Desktop for Current User in C#
Is Using a Mutex to Prevent Multiple Instances of the Same Program from Running Safe
Order of Event Handler Execution
Are Static Class Instances Unique to a Request or a Server in ASP.NET
Which Radio Button in the Group Is Checked
Using' Statement VS 'Try Finally'
Get All Inherited Classes of an Abstract Class
C# Pass by Value VS. Pass by Reference
Does It Make Sense to Use "As" Instead of a Cast Even If There Is No Null Check
Should I Call Close() or Dispose() for Stream Objects