How to watch live objects at run time - c#

Is there some way to watch all my in memory (non-disposed) objects ?
this can help in complex dirty old apps to keep track of memory and find the leak reasons faster
thank you.

You'll want to use a profiler to track object allocation/de-allocation over the run of an application. Take a look at the Visual Studio Profiler or a commercial one like Red Gate ANTS memory profiler or JetBrains dotTrace. CLR Profiler is another useful tool specifically for this purpose.

Related

can we know which are the variable/ that are causeing memory leakage issue in running exe in c# .net

Currently I am running one .exe
I causes memory leakage issue. So I want to know how we could know which variable or process is responsible for this issue
I recommend getting a good memory profiler. I've used Redgate ANTS in the past. It is very good. JetBrains dotMemory is an alternative to this. If you need to profile your application then there are lots of options. A good profiler should be able to tell you what objects are growing over time

Any way in C# / .NET app to get breakdown of memory allocated to each type in memory?

I have an app which consumes a lot of real time data, and because it's doing so much it's quite slow under the VS 2010 and this causes it to fail in various ways.
So I was wondering if there's any way other than this profiler that I can find out how much memory in bytes say is allocated to each type in memory and dump this out periodically?
It's quite a large application so adding my own counters isn't really feasible...
You need to use a memory profiler.
There are many around, some free and some commercial.
MemProfiler
ANTS memory profiler
dotTrace
clr profiler
Also see What Are Some Good .NET Profilers?
There is no easy general purpose way of saying GetBytesUsedForInstance(object), but it depends what you need the data for (unless all your types are value types, in which case it should be relatively simple).
We have an in memory cache for part of our application. We care most about relative amounts of memory used - ie the total cache size is twice what it was yesterday. For this, we serialize our object graphs to a stream and take the stream length (and then discard the stream). This is not an accurate measurement of "how much memory a type uses up" per se, but is useful for these relative comparisons.
Other than that - I think you are stuck using a profiler. I can highly recommend SciTech Memory Profiler - I use it a lot. It integrates well into Visual Studio, is fast (the latest version is anyway), and gives tremendously useful detail.
I would suggest like for getting a general information massively use Process Explorer.
One time you gfigure out you need to understand the stuff deeper (what kind of objects are on heap, for example) , the best tool I used for profiling is JetBrains Memory and Performance profiler. This one is payed only.
If you need only performance profiler, there is really good free option Equatec Performance profiler
Good luck.

library/API recommendation for adding instrumentation in code

If I want to add some instrumentation in my code for getting metrics like how long a particular method took to execute, anyone has recommendation for either windows build-in or any 3rd party library?
I've seen this done with PostSharp AOP framework, hooked into Windows Performance Counters (System.Diagnostics.PerformanceCounter). One of their introductory pieces of sample code shows how to do this:
http://www.sharpcrafters.com/solutions/performance
This might be a good option if you want to deploy these measurements to production and measure performance in a live manner (to do diagnosis of a running system).
If you just want to locate bottlenecks in your code, and don't need to do live diagnosis, I suggest you simply use a profiler, such as the one built into VS or Ants Profiler (as Chris already suggested). Then run a redacted copy of your production data against your code so your performance measurements match production.
If you want this at runtime, you should probably use a profiler offline. I'd avoid doing any instrumentation where possible as it takes time and therefore causes more performance problems.
Google for RedGate ANTS Profiler or JetBrains dotTrace. If you have VS2010 Premium, it has one built in. That will give you a method-level breakdown of what is happening time-wise.
Generally a profiler will take care of measuring performance.
If you need to measure time yourself StopWatch with some sort of tracing is often enough.

Software for testing applications to increase performance

I'm searching for software to do benchmarking, analysis, performance of code and apps.
Something like Intel VTune.
Can anyone give me some names, free or payed, targeting c# apps.
thanks
Redgate Performance Profiler
...their Memory Profiler will undoubtedly help as well.
It sounds like you have multiple purposes.
If you want to monitor the performance health of programs, benchmarks and profilers that measure are probably what you want.
If you need to make a program faster, and you have the source code for it, I think your best bet is to get something that samples the call stack and gives you line-level percent of wall-clock time. For this, don't look for high precision of timing. Look instead for high precision of pinpointing code that is responsible for high percentages of time. Especially in larger programs, these lines are function or method calls that you can find a way to avoid. Don't fall into the myth of thinking that the only problems are hotspots where the program counter lives, and don't fall into the myth that all I/O is necessary I/O.
There's a totally manual method that a few people use and works very well.
Visual Studio, in it's higher SKUs, includes multiple profilers which can be used for this purpose. If your application is using multiple threads, VS 2010's new Concurrency Profiler is incredible for profiling C# concurrent code.
There are many other performance profilers on the market which are useful for performance profiling as well. I personally like dotTrace and think it's a very clean interface.
Such a tool is usually called a Profiler.
The (free) MS CLR Profiler (up to Fx 3.5) isn't bad, and a good place to start.
Update: a 2022 Blog about this.
I've used the EQATEC profiler which is free for personal use. It benchmarks run time and helped point out the inefficiencies that were causing bottlenecks in my code.

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.

Categories