How do you log all garbage collection events in CLR/.Net? - debugging

I'm looking for an equivalent of java -verbose:gc or any tool or code snippet to get the same information. Ideally, this would be something I could run in an unattended fashion on a service and log everything to a file. My use case is profiling GC-induced latency in a long-running service.

For noninvasive .NET GC profiling you have few options. You can either use CLR Memory Performance Counters or CLR Memory Event Tracing or some profiler (SciTech memory profiler has a nice command line tool that allows you to collect CLR profiling data in the production environment - other .NET profiles probably also expose such a feature).
I suppose that Performance Counters are the least invasive method, but they don't give you a detailed information on GC working - though you can see how many collections were performed (in each generation) as well as how much time your process spent in GC. To collect this information you may use perfmon, typeperf or Powershell (I once described different ways of using perf counters so you may have a look: http://lowleveldesign.wordpress.com/2012/04/19/diagnosing-applications-using-performance-counters/)
ETW events provide much more details on GC inner workings. You can configure the ETW provider manually (using logman or xperf for example) or use an excellent tool PerfView (as #Marc pointed in a comment). If you are only interested in GC events, check GC Only checkbox in the Collect windows:
There is a great episode of Defrag Tools dedicated to CLR GC profiling (part 4): http://channel9.msdn.com/Shows/Defrag-Tools/Defrag-Tools-36-CLR-GC-Part-4, I also recommend you checking the other parts as well as reading the PerfView documentation. PerfView is a really powerful tool and it even allows you to analyse .NET Heap and compare memory snapshots.
The last option (that is using a memory profiler) is probably the most invasive of the three methods, but sometimes might give you even more details on GC heaps (especially when you would like to analyse the objects graphs). I can't think of any good free GC Memory Profiler so probably you will need to pay to get one of those tools. I have some experience with SciTech Memory Profiler (it's pretty good and, as I mentioned earlier, they have a command line client that allows you to collect data on production). I also tried Visual Studio Memory profile - it's not bad but less powerfull than the SciTech one - and finally JetBrains and RedGate also sell memory profilers which are well know among .NET developers and probably comparable to SciTech.

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.

windows performance recorder record specific process

Using the Windows Performance Recorder, is it possible to generate an ETL file based on the tracing of a single process? The ETL files generated for all of the processes in the system result in ETL files measured in GBs for intervals as small as a couple of minutes.
ETW (kernel event) tracing is system wide and captures all processes.
I don't think it is possible to record ETW traces that record just one process (at least not with xperf or wpr). If your traces are too big then the best tactic is to make sure that the rest of the system is as quiet as possible so that it doesn't contribute too much data.
If the rest of the system is already quiet then the traces are probably big because ETW traces tend to be big. You can use trace compression to make them smaller on disk - see UIforETW for how this works - https://randomascii.wordpress.com/2015/09/24/etw-central/.
If the rest of the system is not already quiet then yes, it probably is contributing to bloat in the traces. Note that it may also be affecting performance, so that data is not irrelevant.
And, if you really do need single-process profiling consider using a different profiler. The Visual Studio profiler does per-process profiling.

Crash dump collection and Performance Impact

Currently, I am searching for a solution that would allow me to monitor a .NET Windows Service Application, in the production environment, and collect memory dumps. I'd like to collect them based on some specific thresholds, at will and on application crashes. I am aware of various methods to achieve this, such as:
DebugDiag
Procdump
Through WER
ADPlus
WinDbg etc.
Some of the methods facilitate the collection during a crash, such as option #3, while others can trigger based on performance counters, such as #1 and #2. Any non-invasive debugger can help me achieve the collection, but I am not sure what's the performance impact of having one attached. For example, if I use Procdump with the -e switch to collect memory dumps on unhandled exceptions, what would be the overhead for the monitored application? Bear in mind that I am referring to a production environment.
I'd be grateful if I you could point me a source or method that explains the performance impact of attaching a non-invasive debugger for memory dump collection. Ideally, that'd be a quantitative measure, although it exceeds my expectations.
P.S: I am not referring to the time needed for the memory dump to be written in the disk, where the application is completely frozen. That's another thing.

Measure CPU / RAM usage of a program

could anyone suggest a way (other than using Task Manager) to track and log a program's usage of CPU and RAM in order to profile its performance?
I'm working under Windows.
Something generic would be useful. A more specific request solution would involve Visual Studio. I've tried Performance Wizard, but it doesn't seem to give me the information I need. Thanks
Process Explorer can be useful.
You can use perfmon utility to gather various counters
Well, there are published APIs for that sort of thing. You might want to take a look at WMI and the Win32_Process class.
If you're looking for a command-line program that gets those things for you there is tasklist and wmic. You can parse their output if you're so inclined.
The Microsoft Platform SDK includes the Windows Performance Toolkit, which tracks CPU, disk, and memory usage over time (along with a ton of other features). It's very handy for tracking down spikes of CPU/memory usage, as well as tracking down issues like why your laptop won't sleep.
How about Intel VTune?
I view the measuring of performance, and the finding of performance problems so as to make the program faster, as two distinctly different goals.
For measuring, one can use profilers, or simply timers, to get the job done.
For finding performance problems, I take an entirely different approach.

Is a thread's stack reported as memory used in Task Manager?

My coworkers and I are trying to track a memory issue in an application, and in my research I found a blog entry that talks about how each thread gets a 1MB stack by default. Our application happens to create a lot of threads, and so we wrote a quick test program to make sure we understood exactly what was happening. The test app (C#) just goes and creates 300 threads, but Task Manager still only showed 22MB of memory. Is stack memory not counted by Task Manager, or is something else going on?
Task Manager is not the best tool for memory consumtion determination. Instead, download the free trial of a tool like MemProfiler, or RedGate's Memory Profiler
Don’t use the mem usage column in Task Manager for diagnostics or profiling. Use the Perfmon counters, especially Private Bytes and the specific .NET counters that will reveal problems like memory leaks.
Might also be of interest: Memory Usage Auditing For .NET Applications

Resources