How to analyse program's memory usage in Visual Studio 2010? - visual-studio

Is there a way to check what memory has been allocated to in a program coded in VS 2010?
I've been noticing that a recent program I've been making keeps using more and more memory over time without releasing any.
Also, are there any articles on code design that teaches you good memory management? I seem to be searching the wrong terms on google as I keep getting useless results.

There are commercial tools such as ANTS Memory Profiler or .NET Memory Profiler or dotTrace. Secondly, take a look at this MSDN article. Lastly, Maoni Stephens has many blog posts on garbage collection and memory management in .NET. They are all linked here.

Related

what is normal for a site to use in terms of cpu and memory

We have a few sites running MVC3 on a windows 2008 server. We're seeing that in average these sites are using over 300 mb in memory each one, and high CPU. Each site easily goes to 20-25% CPU when they get requests.
Is this normal?
I know my question is very general, but if we were to spend time on optimization, etc.. what should we aim for? what is considered normal in terms of memory and cpu usage for a typical database driven MVC3 website?
Also, I was told that we should "profile" the application to troubleshoot the high CPU usage? Is this done via Visual Studio, or through some other tools?
thanks for your help in advance,
G.S
Without knowing the details of your application and what it's doing, it's impossible to say what's normal and what isn't. If performance is a problem, you should optimize, if it isn't, you shouldn't :)
Profiling is a general term for measuring the performance of different aspects of your application. You can profile memory, cpu usage, garbage collections and thread use (among other things) using a profiler.
There are several profilers around such as Ants, .Net memory profiler and some excellent ones built into visual studio. They are available in the pro versions of VS2012 and up.

How do I find out what Visual Studio extension is causing memory leaks?

My Visual Studio (2010 SP1) has a serious memory leak, and it seems to be caused by one or more of the installed extensions.
I have tried to narrow it down by turning extensions and add-ons on and off, but it takes a while of work to build up a significant leak and the results aren't totally conclusive. It rarely takes more than a few hours before the devenv.exe process uses 2GB memory and starts trashing, which is becoming a bit of a pain to work with.
Is there some way to make VS emit information on memory allocated for extensions?
Are managed extensions running in their own AppDomain? Maybe that would allow me to turn on some performance counters that could help pinpoint the problem.
Anything else I can do to troubleshoot this apart from disabling stuff one by one until the problem disappears?
According to the JetBrains faq page you should be able to attach to an already running process when it starts to display memory leak issues. Perhaps you can give that a shot. Note that the faq states that you need to be using version 5.

VS2010 for debugging com interop memory leaks

I know there have been some major improvements in Visual studio 2010 for debugging things like memory leaks from dump file etc. I wonder if it has any improvements specifically debugging memory leak for COM interop scenarios?
There's no specific way to test for memory leaks in unmanaged code in VS, especially from a managed host point of view. It gets even worse if you have no access to the unmanaged code.
My suggestion would be to create a bunch of instances of the COM object and repeatedly call its methods then dispose of the objects. If the memory usage just keeps increasing, there's probably a memory leak.
Then the real trick is to find it! There are a lot of ways to do this (way too many to enumerate here) so I'd suggest a quick Google for memory leak debugging tools.

Free .NET Profiler for .NET 4.0 mixed code

I checked out some of the performance profilers mentioned here. But...
EQATec didn't work for me because I have many assemblies I want to profile, and it has a limit on assemblies to profile. How much of a hassle is getting a free license? I'd go for it if someone guaranteed me that EQATec can profile both managed and unmanaged code
SlimTune only profiled my managed code, even if I set "Profile native functions" to "True"
XTE Profiler is no longer free
We have a copy of AQTime 6 we bought before, but it doesn't seem to support .NET 4.0 apps (it can't even start my app)
We use Visual Studio 2010 Professional SP1, so we don't have the Visual Studio profiler
I tried the "poor man's profiling" (halting the program many times and seeing where it is), but I get way too random results and I'm more used to traditional profiling
(I've spent the whole day stumped on this, sorry if I was too negative)
UPDATE: After I cleaned my solution, built it again and checked all debug info (.pdb) was copied to the same directory as the executable, I tried AQTime again and it worked! It showed me routine timing info for both managed and unmanaged code, so my problem is solved. However, I'm using a paid profiler, so the question will remain open until I take a look at xperf or someone comes up with something else
AQTime have a free version of their latest profiler (http://smartbear.com/products/free-tools/aqtime-standard/) It supports .Net 4, But I doubt it can do a mixed profile of Native and Managed.
If you are really serious about it you might look into the Microsoft xperf tools (http://msdn.microsoft.com/en-us/performance). They have a steep learning curve but they are free and I doubt any commercial profiler can do what xperf can (the instrumentation is in the OS, not in a separate process, thus either Vista, win7 or win2K8 are required). I'm waiting for someone to write a nice GUI around it, but it's taking a bit long... ;-)
xperf will profile your native code and you can load your symbols into the result viewer. I don't think it will go down to per-line granularity though. It has has a .Net CLR Provider (http://msdn.microsoft.com/en-us/library/dd264809.aspx). The cool thing about xperf is that it can also show other processes that may be influencing your performance (you are free to switch it off and only profile your own process). For example: it is capable of revealing that your IO is slow due to a badly written USB driver, virus scanner or firewall software. A traditional profiler would only show the slow IO, causing you to focus on a non-bottleneck.
By the way, there is also an ICorProfilerCallback interface you can utilize to write your own profiler (http://msdn.microsoft.com/en-us/library/s5ec0es1.aspx).
I' pretty sure the answer to your question is "There isn't one".
In comparing a whole bunch of .NET profilers a few months ago I found only very few could do mixed .NET/native profiling: AQTime ($599) and Glowcode ($499) could. Or so they say - I didn't try it.
EQATEC, Visual Studio, ANTS, Jetbrains dotTrace, Yourkit, XteProfiler, Slimtune etc could not, so I doubt you'll find a free profiler anytime soon that can.
CLR Profiler 4 by Microsoft is free. Have you tried it?
What do you look for in the unmanaged part of the profiler?
Your concern about EQATEC Profiler is easily resolved: it only does managed .NET profiling, not at all any kind of unmanaged profiling.
As shown in the pricing the actual profiling functionality differs only in the number of assemblies that can be profiled at once. So a $0 Free edition profiles a single-assembly WP7-app just as fine as a $999 Corporate edition does. For the extra price tag you get to profile more assemblies at once and a handful secondary features, like print, compare, min/max etc.
Getting a free license by trying out EQATEC Analytics is said to be easy. Going for the unlimited Corporate license is quite a popular choice and many have achieved it in just a couple of hours. Getting a free $99 Standard license shouldn't take more than 10 minutes or so, if you're good. Please note: I work at EQATEC and we actually hand out so many free licenses every day now that's it's almost become a burden because each is manually processed (yes, seriously!) so this particular offer may not go on forever.
OP: "I'd go for it if someone guaranteed me that XXXX can profile both managed and unmanaged code"
Our C# Timing Profiler is not dependent on how your C# code is compiled (managed or unmanaged, or mixed). It should work fine for this.

Code Profiling in Visual Studio 2005

I have a Visual Studio 2005 Solution workspace which in turn has 8 projects included in it. I want to profile the complete code(all the projects) and get some measure about the absolute cycles taken by each function to execute, or at least percentage cycle consumptions.
I checked out help for VS 2005, and also the project setiings options but could not find any pointers on hwo to get the profile info.
Any help regarding this would be beneficial.
-AD.
If your application is not particularly processor intensive, redgate ANTS Profiler is a good choice - the line-by-line stats can come in quite handy, and the whole product is clean and well-designed.
If your app needs a lot of CPU to operate normally, however, most of the .NET profilers on the market won't be able to handle it. The only two that I have ever found that will work for a really heavy-weight application are JetBrains dotTrace and YourKit. The two are very similar, which is not surprising, given that YourKit seems to have been started by a former JetBrains employee. I personally prefer dotTrace, but that may just be because that is what I used first, and there has never been any good reason to switch.
I have tested ANTS, AQTime, DevPartner, GlowCode, Borland OptimizeIt and Intel VTune, and all of them have too much overhead to handle a demanding application. (VTune is a possible exception, but it is so horribly complex to configure and use that I was never able to figure out exactly what it could handle. It is also very expensive.)
I guess the inbuilt profiler of Visual Studio 2005 comes onyl with the Developer Edition and Team Edition. I have a Professional edition which, it seems doesnot have the inbuilt profiler tool.
-AD
I've used both the profiler in Compuware’s DevPartner (I like to still call it “TrueTime”) and Rational's Quantify. I always liked Quantify better, but as I've moved between companies DevPartner is usually already the “standard”.
Both are expensive, but they (seem to) add so much value that any commercial shop should have no problem investing in some seats.
Quantify didn’t require special rebuilds of the project – which was GREAT. It also crashed less (that’s not saying much, it had its own issues). DevPartner also tends to break as each new version of Visual Stuido was release (maybe this is better now?). Buy the yearly maintenance agreement if you go this way.
That said, I’ve often just write a class remembers the time at construction and spits out (log file) the elapsed time in its destructor. I used QueryPerformanceCounter. I’d stick this class at the top of the function I’d want to time. You could get fancy with making it a macro, use the preprocessor to include this class only under a special build…
I recommend you EQATEC profiler which also includes in its site a tracer.
Also it's free and easy to use.
alt text http://www.eqatec.com/tools/profiler/profiler-logo.gif
We use DevPartner with Visual Studio 2005. It gives you performance analysis of the specific projects in your solution you want to look at. We also use it for memory management analysis, and error analysis. Is commercial tool, so it's not free.
Red-gate's Profiler is great for this.
I use Jebrains profiler is very easy to use and performs very well too.
If your app needs a lot of CPU to operate normally, however, most of the .NET profilers on the market won't be able to handle it.
I have used a trial version of RedGate Ant's profiler on an optimizing algorithm that normally uses up to 100% CPU on a single core machines and though slow it managed to get through and report all I needed to know. Extremely helpfull tool. I wonder what kind of algorithms have you run on the Ant's profiler.
Has anyone used the VS profiler ?

Resources