c# vs2005 .net 2.0: code optimization - c#

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.

Related

Tools for detecting memory leaks for C# and unmanaged C++ application

We have an application which is developed in the C# .NET Framework 2.0 that is interacting with a COM component(which is developed in C++ unmanaged code). Sometimes the application throws an out of memory exception(quite hard to reproduce).
We would like to have a tool to find out if we have any memory leaks, and identify the root cause of the memory exception. What tools would be best for this? An ideal tool would be able to attach to a running process on a user's machine to do analysis.
We have tried .NET memory profiler but that can only attach to a .NET framework 4.0 running process. We also used Memory Validator (C++ memory leak detector); however, this tool didn't give us enough hints when attaching to a running process.
Use a memory profiler - they can help to find such leaks, for example:
http://memprofiler.com/ (commercial, managed PLUS unmanaged integrated in one profiler!)
http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/ (commercial)
CLR Profiler from MS (free)
Short answer
Have you tried Microsoft Application Verifier? It is a very small, I'd daresay minimalistic, but surprisingly powerful tool.
Long answer
During my investigation of the memory leak issues, I have used Microsoft Windbg, Microsoft Application Verifier and I have thoroughly evaluated Red Gate ANTS Performance Profiler (which I have recommend to buy, and I have personally seen recommended to buy by various developers in many different teams), DotTrace and .Net Memory Profiler.
There is definitively value to get and learn how to use one or more of such tools, perhaps then installed on a virtual development environment hosted on some virtual machine pooled between the developers of the team.
If you evaluate those and any other tool, the big selling point for me would be the ability to investigate memory dumps taken with Windbg, as those are the most easily obtainable even from customers.
try WinDbg and this receipt: http://www.codeproject.com/KB/cpp/MemoryLeak.aspx
For the most basic tests, you could use sysinternals procexp.exe to check if you really have a memory leak problem. A lot of COM code would throw out-of-memory, when a null pointer is encountered where one wasn't expected, so the exception code may be misleading.

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.

Performance and monitoring .NET Apps

We've build a Web Application which is performing horrible even with alot of resources available. My boss doesn't believe me that the application is consuming alot of Hardware IO, so I have to prove that the hardware is ok, but the web app is really crap.
The app is using:
SQL Server 2000 with SP4
The main web application (.NET 3.5)
Two Web Services (.NET 1.1)
Biztalk 2004
There are 30 people using this apps.
How can I prove I am right?
You can hook up a profiler like ANTS profiler or JetBrains DotTrace and see where the application's performance bottlenecks are.
One place you could start is getting a performance profiler like Red-Gate ANTS profiler. I've used this tool and it's very useful is weeding out performance bottlenecks.
Randy
You could start by using SQL Server Profiler to get an impression of the amount of database traffic that is going on.
I'm not saying that database interaction is the bottleneck, but it often is, and the tool is already there if you are using SQL Server, so it may be a good idea to take a look at that before you go out and buy a lot of profiling tools.
Visual Studio 2008 also have built-in performance analysis tools.
Windows performance counters are a good way to get some basic information about general system performance. Proper counters will show you if it's really the IO that's doing lots of stuff. If you take out the numbers from the counters and compare those to the specs, you should be able to tell if the system is maxing out or not.
If the system is maxing out, it's a problem with the web application, and it should be profiled to find out where to start optimizing.
You could use the system performance monitor built into windows since at least XP. You can get almost any information you could possibly need. This includes CPU time, .NET memory usage (include gen0 gen1 and gen2), native memory usage, amount of time spent garbage collection, disk access time, etc. If you just search codeproject or just the web there are many examples of using these counter to test for just about anything you want.
One of the benefits of this is you don't have to change your code and can be used with existing system.
I find this is the best starting point to point you to where you should look for bottle necks and issues.

C# runtime application Diagnostics

I am developing a c# GUI and was wondering if there were any (preferably free) runtime diagnostic programs available.
What Im looking for is a way to monitor user interactions with my GUI and what functions are called when. Preferably I do not want to add stacks of debug code as this has the potential to change the behaviour of the bug I am looking for.
Currently Im using Spy++ but am finding it a little heavy going as Im kinda new to this and its generating stacks of data.
EQATEC profiler is pretty simple to use. Free too. Started life targetting Compact Framework but since 2.00 is now geared more towards Desktop too.
I recommend ANTS profiler.
There is a trial available, first two weeks free.
In addition to ANTS profiler have a look at JetBrain's dotTrace

How to calculate/save memory useage of .NET app on terminal servers?

I do some C# fat application on Citrix/Terminal Server.
How to measure actual memory usage per-session?
What can I do to reduce the memory usage in total?
We still working on .NET 1.1. Does it have a difference if we upgrade our .NET runtime?
Its very difficult to get metrics on actual memory usage in .NET. About the closest approximation you can get is on a per-object basis by calling Marshal.SizeOf(). My understanding of that method is that it is essentially measuring the size of a serialized version of the object, and the in-memory footprint may be close to that, its not exact. But its a good estimate.
You may also want to investigate the SMS API's under .NET. They provide ways to query various memory statistics from the Operating System about your process (or other processes). Its the same library that is used by "perfmon". You may be able to use that to inspect your process programatically.
Also, you'll want to invest in a good profiling tool for .NET. I've evaluated ANTS and dotTrace. They are both very good. I preferred dotTrace for its simplicity. Most profilers have very non-intuitive interfaces. That's something I've just come to expect. dotTrace is actually quite good, by those standards. ANTS I think is probably more advanced (not sure, just my opinion from the brief eval I did on both of them).
Here is an article from MSDN on measuring application performance. It includes doing measurements on memory usage.
I don't think upgrading the .NET Runtime will have a significant effect on the application as it is, you will probably be better off optimising your application in other ways. Try to dispose of resources when you don't need them.

Categories