Tools for Memory leaks in .Net executable [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What Are Some Good .NET Profilers?
I am trying to test an application in Windows for memory leaks. I have looked at Linux alternatives (eg. Valgrind) and I am looking for a similar tool for Windows. The problem with .Net is that the memory is not released straight away like in Linux.
My knowledge of .Net is limited so my problem is I would like to use a tool that would find memory leaks for a Windows .Net executable. Ideally the program should be able to detect any memory leaks while I use the application. Does anyone know of such a tool or have any suggestions to solve this issue?
Thanks for any help in advance.
Thanks for the responses. Can anyone tell me if any of these program's allow you to execute from the command line and output memory leak reports into a text file.
Thanks

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.

Some others have already posted good links to profilers -- redgate's profiler is particularly good.
Here's a link to a good article on finding leaks in .Net:
http://blogs.msdn.com/b/ricom/archive/2004/12/10/279612.aspx
Here's a great blog -- the author is a support engineer at MS (these people are really good programmers that work in windebug all day to find problems). A great many of her articles are about trackign down memory leaks. Most of her examples are asp.net, but most of the techniques should apply to Windows apps as well:
http://blogs.msdn.com/b/tess/archive/2006/01/23/516139.aspx
Also, be aware that you might not have a real leak. By design, the .Net garbage collector does not immediately release memory. It does periodic collections that are triggered by various events (I don't think that MS has published a complete list of the things that will cause the garbage collector to fire). I do know that one of the triggers is low memory conditions, and another trigger can be a memory allocation. If nothing triggers it, the GC will not collect.
That means that you can start a Winform .net app, let it eat a bunch of memory (and then release it), and then leave it sitting all night long. If the computer has been idle for the whole time, the GC may not have fired, and the app still might own a lot of memory. When something happens to trigger the collection, memory will be freed. It's very common that people report that .net apps appear to be slow to release memory as a result of this behavior.
Go ahead and profile -- you might have a leak, but also be aware that this might be behavior by design. Either way, good luck!

We have tried a lot of the memoryprofilers for .NET and have come to the conclusion that the .NET memory profiler is the best one currently on the market with a good combination to ease of use, amount of availabele data and perfomance.

Related

Xamarin iOS Memory Profiling

I have an app that is nearly ready for release which we have coded in Xamarin.iOS and although it is working really well functionally, the app is having memory leaks left right and centre. This is no doubt a common issue for most developers of Xamarin.iOS as I have read many accounts that say memory management on this platform is not exactly what it may seem to be. Even though I have been disposing views, correctly asking the GC to collect and using dispose methods where I can, it still is leaking memory everywhere with no logical or obvious explanation as to why.
I have come across methods that can support with memory deposition such as this one - Xamarin iOS memory leaks everywhere but I am finding it difficult to identify and be clear on what exactly is leaking memory.
The app is very simple with not many images or text, just almost static informational pages throughout with simple functionality, yet still it is leaking. I am not able to use the Xamarin Profiler to find out what exactly is leaking as I do not have an Enterprise subscription for this and I also find profiling with Instruments to be cumbersome and not very helpful as all it identifies are memory leaks but not exactly what data is leaking or in what method or part of the code.
I would like to know more from the community of what other people use for profiling an Xamarin iOS app and also the bottom line answer - is Garbage Collection working in Xamarin and if there is anything special I must do to get it working, what would that be?
Thank you everyone.
You can use Apple's Instruments for profiling if need be. I'll ask if you're unsubscribing event handlers -- though if it's as big of a problem as you say it is, I have my doubts that event handlers alone could cause the issue. Other common memory leak culprits include singletons and timers. I'll also ask what makes you think you have memory leaks -- perhaps you just have a large app in which case the Xamarin Linker or fewer generics may help.

How to solve Memory leakage in windows multiple threading application

Actually I am working on real-time application with multiple threads.
While I am running my application memory is 65 Mb and if I open a child MDI form then memory increases to 85mb but if I close the child window then memory will still remains at 85mb.
I have used Dispose and I've already tried with GC.Collect(), but none of these solve my problems,
so I am little bit confused regarding this issue.
Can you please guide me regarding this?
Thanks in advance.
You need to get a good memory profiler.
There are a bunch of options:
RedGates's ANTS memory profiler
Scitech's .NET memory profiler
CLR Profiler (pre .NET 4.0)
and more.
Many cost money (except CLR Profiler) but usually have trial versions.
After you start your app (with the profiler attached), you need to take snapshots of the memory before the leak and after and compare them to see what is staying around.
It's hard to say what the problem might be in your case, since there are many things that could be causing the problem.
have you used some global delegate references in your child MDI form?
Does the memory increase every time you open a new child window? If so, open and close several of these and then attach to the process using windbg.
After loading sos, you can use the !dumpheap command to get an idea of where the memory leak might be and then use gcroot to find where the leaked memory's is rooted.
This blog might help:
http://blogs.msdn.com/b/delay/archive/2009/03/11/where-s-your-leak-at-using-windbg-sos-and-gcroot-to-diagnose-a-net-memory-leak.aspx
Btw, how are you writing a "real-time application" in c#?
The task manager memory number is not always as sharp as it would be, however this small trick can cheat that number... Not meant for production.
public static void RefreshMemory() {
try {
Process curProc = Process.GetCurrentProcess();
curProc.MaxWorkingSet = curProc.MaxWorkingSet;
} catch {
// Handle the exception
}
}

How to detect where a Memory Leak is?

I have a large website that seems to be sucking up all the memory that is being allocated. There is nothing else on the server beside this site. Within a week it eats away the 2 gigs and requires a restart. Currently this is server 2008 32 bit using IIS 7. We are reinstalling to use 64bit and add more memory. It would be nice to be able to track down where the leaks are occurring.
So what is the best practice to tracking memory leaks?
Memory leaks in .NET are not that common, but when they happen it is most commonly due to unattached event handlers. Make sure you detach handlers, before the listeners go out of scope.
Another option is if you forget to call Dispose() on IDisposable resources. This may prevent cleanup of unmanaged resources (which are not handled by GC).
And yet another possible reason is a deadlocked finalizer. That will prevent all remaining objects in the finalizer queue from being collected.
I use WinDbg + Sos to track down leaks. The steps are as follows
Dump the heap and look for suspects
Use !gcroot to find out what is keeping the suspects alive
Repeat as necessary
Be aware that large memory usage may also be due to heap fragmentation. The regular heaps are compacted, but pinned objects can cause fragmentation. Also, the LOH is not compacted so fragmentation is not uncommon for LOH.
Excellent tutorials on WinDbg + sos here: http://blogs.msdn.com/tess/
Run a profiler on your code.
Here are two good options:
RedGate's memory profiler
Jetbrains dotTrace
I believe both products have a free trial.
Run, don't walk, over to Tess
Ferrandez's blog, If broken it is,
fix it you should, which has well
scripted labs dedicated to learning
how to diagnose and debug crash, hang
and memory issues with .NET code. She
has some of the best material I've
found to date to help get you started.
Commercial memory profilers such as ANTS and SciTech are excellent resources that will show what objects are in the heap, and how they are rooted. Most commercial memory profilers have the ability to load a memory 'snap' of a process (say from your production environment).
You can capture a memory 'snap' (see Snap v. Dump) using adplus.vbs or DebugDiag. Adplus is available as part of the Debugging Tools for Windows. DebugDiag will also have some rudimentary analysis (but seems to be more reliable on unmanaged code) automagically.
Monitor the Application
For an idea on what to monitor, see Improving .NET Performance and Scalability, specifically Chapter 15.
As to how to monitor, there are commercial tools available for that as well, however, every Windows machine also comes with Perfmon.exe, which can be used to record relevant performance counters.
Test the Application
For an idea on how to perform load, or stress, tests, check out the Patterns and Practices Performance Testing Guidance for Web Applications.
Debug the Application
Once you've identified you've got a problem (monitoring) and your able to reproduce the problem (testing) you can get down to debugging the problem. See the links for Tess - that information will carry you a long way.
Then rinse and repeat! :)
Good luck!
Z
In performance monitor, add counters for Process/Private Bytes and .NET CLR Memory/# Bytes in All Heaps. Private bytes is all memory and CLR memory is just managed. So if the CLR memory remains fairly even but the private bytes continues to grow over time, this means that the leak is in unmanaged resource. That usually means that you are not disposing of native resources properly. A good thing to look at is stuff like COM or IO (streams and files). Make sure all of that stuff gets disposed when you are done with it.
You can try using profilers such as dotTrace -- set it into a memory trace and run your application.
This should give you clues in which assemblies and areas of the application that eat up too much memory on the get go.
This is probably prevention rather then detection, but at the c# code level, you should check that any classes that use large resources such as images and other files are correctly implementing the dispose pattern. If needed you may also need to override the finalizer.
MSDN has good guidance on this subject.
If you have any classes in your application that you know make use of large resources, these are the first places to look for memory issues.
I've found that the EQATEC Profiler is pretty good, plus it's free!
Check out the Memory and Memory Leak labs on this blog post:
.NET Debugging Demos
They may be of some help. Basically, you can use WinDBG to analyze a memory dump and help determine what is eating up all of your memory.
We used a similar approach to determine that Regex was chewing up all of our memory, but only when the product was run on 64-bit machines. The learning curve is kind of steep, but WinDBG is a very powerful tool.
This new article of mine maybe useful: How to detect and avoid memory and resources leaks in .NET applications
Do you do any Office interop? If so, make sure you clean up your application objects. That's one possible culprit.
Another is global objects, such as anything that's static.
Do you have lots of dynamic pages on your site?
You can also try IBM's Purify
I suggest you try with a small set of dynamic pages disabling all others for the meantime. I hate to say this but it's very much possible that IIS 7 may also have leaks.
Look at this article on detecting .NET application memory leaks and related articles mentioned at the bottom of the page and hopefully you will find a solution or at least an idea to resolve it.
Thanks

c# vs2005 .net 2.0: code optimization

What is the best way to see memory leaks or areas to optimize code in your .net source code?
I am using vs2005, c# , .net 2.0
Any good free tools out there that I can safely install on my work desktop?
I don't know of any good C# memory profilers, besides the functionality built into Visual Studios. But a great commercial memory profiler is ANTS Memory Profiler($495)
I've been extensively using the dotTrace profiler from JetBrains (the makers of another invaluable tool, ReSharper). Its fairly lightweight and works well for tracking both memory utilization and standard application profiling.
CLR Profiler can help you troubleshoot memory issues. Beware, though, the learning curve is pretty steep.
http://msdn.microsoft.com/en-us/library/ms979205.aspx
I never had to worry about memory leaks in my .net applications. You have to worry about system resources such as timers, file handles, comm port handles, database connections etc but again if you stick with right pattern (using?), they are not a problem either.
When it comes to optimization, you need to profile your application to see the performance bottlenecks. Don't even rely on your instincts when you are hunting a slow code because you might be looking at the wrong place.
AQTime, Ants profiler are good options but they are not free. They come with a trial period though so if you need them for a short period, you might get away with using them for trial period only. But if you really like them, you can consider buying them.
Or you can consider a free alternative EQATEC Profiler which seems pretty good but I haven't personally used it.

What strategies and tools are useful for finding memory leaks in .NET?

I wrote C++ for 10 years. I encountered memory problems, but they could be fixed with a reasonable amount of effort.
For the last couple of years I've been writing C#. I find I still get lots of memory problems. They're difficult to diagnose and fix due to the non-determinancy, and because the C# philosophy is that you shouldn't have to worry about such things when you very definitely do.
One particular problem I find is that I have to explicitly dispose and cleanup everything in code. If I don't, then the memory profilers don't really help because there is so much chaff floating about you can't find a leak within all the data they're trying to show you. I wonder if I've got the wrong idea, or if the tool I've got isn't the best.
What kind of strategies and tools are useful for tackling 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.
Just for the forgetting-to-dispose problem, try the solution described in this blog post. Here's the essence:
public void Dispose ()
{
// Dispose logic here ...
// It's a bad error if someone forgets to call Dispose,
// so in Debug builds, we put a finalizer in to detect
// the error. If Dispose is called, we suppress the
// finalizer.
#if DEBUG
GC.SuppressFinalize(this);
#endif
}
#if DEBUG
~TimedLock()
{
// If this finalizer runs, someone somewhere failed to
// call Dispose, which means we've failed to leave
// a monitor!
System.Diagnostics.Debug.Fail("Undisposed lock");
}
#endif
We've used Ants Profiler Pro by Red Gate software in our project. It works really well for all .NET language-based applications.
We found that the .NET Garbage Collector is very "safe" in its cleaning up of in-memory objects (as it should be). It would keep objects around just because we might be using it sometime in the future. This meant we needed to be more careful about the number of objects that we inflated in memory. In the end, we converted all of our data objects over to an "inflate on-demand" (just before a field is requested) in order to reduce memory overhead and increase performance.
EDIT: Here's a further explanation of what I mean by "inflate on demand." In our object model of our database we use Properties of a parent object to expose the child object(s). For example if we had some record that referenced some other "detail" or "lookup" record on a one-to-one basis we would structure it like this:
class ParentObject
Private mRelatedObject as New CRelatedObject
public Readonly property RelatedObject() as CRelatedObject
get
mRelatedObject.getWithID(RelatedObjectID)
return mRelatedObject
end get
end property
End class
We found that the above system created some real memory and performance problems when there were a lot of records in memory. So we switched over to a system where objects were inflated only when they were requested, and database calls were done only when necessary:
class ParentObject
Private mRelatedObject as CRelatedObject
Public ReadOnly Property RelatedObject() as CRelatedObject
Get
If mRelatedObject is Nothing
mRelatedObject = New CRelatedObject
End If
If mRelatedObject.isEmptyObject
mRelatedObject.getWithID(RelatedObjectID)
End If
return mRelatedObject
end get
end Property
end class
This turned out to be much more efficient because objects were kept out of memory until they were needed (the Get method was accessed). It provided a very large performance boost in limiting database hits and a huge gain on memory space.
You still need to worry about memory when you are writing managed code unless your application is trivial. I will suggest two things: first, read CLR via C# because it will help you understand memory management in .NET. Second, learn to use a tool like CLRProfiler (Microsoft). This can give you an idea of what is causing your memory leak (e.g. you can take a look at your large object heap fragmentation)
Are you using unmanaged code? If you are not using unmanaged code, according to Microsoft, memory leaks in the traditional sense are not possible.
Memory used by an application may not be released however, so an application's memory allocation may grow throughout the life of the application.
From How to identify memory leaks in the common language runtime at Microsoft.com
A memory leak can occur in a .NET
Framework application when you use
unmanaged code as part of the
application. This unmanaged code can
leak memory, and the .NET Framework
runtime cannot address that problem.
Additionally, a project may only
appear to have a memory leak. This
condition can occur if many large
objects (such as DataTable objects)
are declared and then added to a
collection (such as a DataSet). The
resources that these objects own may
never be released, and the resources
are left alive for the whole run of
the program. This appears to be a
leak, but actually it is just a
symptom of the way that memory is
being allocated in the program.
For dealing with this type of issue, you can implement IDisposable. If you want to see some of the strategies for dealing with memory management, I would suggest searching for IDisposable, XNA, memory management as game developers need to have more predictable garbage collection and so must force the GC to do its thing.
One common mistake is to not remove event handlers that subscribe to an object. An event handler subscription will prevent an object from being recycled. Also, take a look at the using statement which allows you to create a limited scope for a resource's lifetime.
This blog has some really wonderful walkthroughs using windbg and other tools to track down memory leaks of all types. Excellent reading to develop your skills.
I just had a memory leak in a windows service, that I fixed.
First, I tried MemProfiler. I found it really hard to use and not at all user friendly.
Then, I used JustTrace which is easier to use and gives you more details about the objects that are not disposed correctly.
It allowed me to solve the memory leak really easily.
If the leaks you are observing are due to a runaway cache implementation, this is a scenario where you might want to consider the use of WeakReference. This could help to ensure that memory is released when necessary.
However, IMHO it would be better to consider a bespoke solution - only you really know how long you need to keep the objects around, so designing appropriate housekeeping code for your situation is usually the best approach.
I prefer dotmemory from Jetbrains
Big guns - Debugging Tools for Windows
This is an amazing collection of tools. You can analyze both managed and unmanaged heaps with it and you can do it offline. This was very handy for debugging one of our ASP.NET applications that kept recycling due to memory overuse. I only had to create a full memory dump of living process running on production server, all analysis was done offline in WinDbg. (It turned out some developer was overusing in-memory Session storage.)
"If broken it is..." blog has very useful articles on the subject.
After one of my fixes for managed application I had the same thing, like how to verify that my application will not have the same memory leak after my next change, so I've wrote something like Object Release Verification framework, please take a look on the NuGet package ObjectReleaseVerification. You can find a sample here https://github.com/outcoldman/OutcoldSolutions-ObjectReleaseVerification-Sample, and information about this sample http://outcoldman.com/en/blog/show/322
The best thing to keep in mind is to keep track of the references to your objects. It is very easy to end up with hanging references to objects that you don't care about anymore.
If you are not going to use something anymore, get rid of it.
Get used to using a cache provider with sliding expirations, so that if something isn't referenced for a desired time window it is dereferenced and cleaned up. But if it is being accessed a lot it will say in memory.
One of the best tools is using the Debugging Tools for Windows, and taking a memory dump of the process using adplus, then use windbg and the sos plugin to analyze the process memory, threads, and call stacks.
You can use this method for identifying problems on servers too, after installing the tools, share the directory, then connect to the share from the server using (net use) and either take a crash or hang dump of the process.
Then analyze offline.
From Visual Studio 2015 consider to use out of the box Memory Usage diagnostic tool to collect and analyze memory usage data.
The Memory Usage tool lets you take one or more snapshots of the managed and native memory heap to help understand the memory usage impact of object types.
one of the best tools I used its DotMemory.you can use this tool as an extension in VS.after run your app you can analyze every part of memory(by Object, NameSpace, etc) that your app use and take some snapshot of that, Compare it with other SnapShots.
DotMemory

Categories