.Net Performance by Bijoy Singhal


Published on

Published in: Technology
1 Comment
1 Like
  • A well written article.In.net performance Event-driven may feature asynchronous transfer of notifications when the calling thread is not blocked. Both data recalculation and repainting consume CPU resources in different proportions with different number of rows.For more information read this.
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • .NET is a powerful platform to build mission critical, robust enterprise applications. While, .NET makes it very easy to build almost any kind of application, it is important that we as developers understand what is going on under the hood. Know, how the architecture of .Net should be leveraged in the best possible way and are aware about the tools available to measure and optimize the performance of our .NET applications. In this session – we will share some secrets to write performant .NET applications. We will touch upon performance counters, performance monitor, CLR profiler etc…
  • IndicatorsMemory Leaks Indicators: # bytes in all Heaps increasingGen 2 Heap Size increasing# GC handles increasing# of Pinned Objects increasing # total committed Bytes increasing# total reserved Bytes increasingLarge Object Heap increasing Virtual Address Space Fragmentation Indicators: # total reserved Bytes significantly larger than # total committed Bytes# of Pinned Objects increasing# GC handles increasing# bytes in all heaps always increasing. CPU Spin Indicators: Current Queue Length is very close to Threads Count and stays that way for a long time.% Processor Time is continuously at a fixed level for a long period of time (as long as the Current Queue Length is at the same value). Managed Deadlock Indicators: Current Queue Length is very close to Threads Count and stays that way for a long time.% Processor Time is 0 (or close to 0) (as long as the Current Queue Length is at the same value) and the application stopped responding. Blocked Finalizer Thread Indicators: # bytes in all heaps increasingPrivate BytesincresingVirtual Bytes increasing
  • When is Exception Thrown.:I can think of 3 reasons off the top of my head. Exceptions are expensive. Exceptions can take you into unnecessary code paths. Exceptions are generally thrown when something went wrong. 2. Cost:Grab a stack trace by interpreting metadata emitted by the compiler to guide our stack unwind. Run through a chain of handlers up the stack, calling each handler twice. Compensate for mismatches between SEH, C++ and managed exceptions. Allocate a managed Exception instance and run its constructor. Most likely, this involves looking up resources for the various error messages. Probably take a trip through the OS kernel. Often take a hardware exception. Notify any attached debuggers, profilers, vectored exception handlers and other interested parties. Resources:1. http://blogs.msdn.com/tess/archive/2005/11/30/are-you-aware-that-you-have-thrown-over-40-000-exceptions-in-the-last-3-hours.aspx 2. http://msdn.microsoft.com/en-us/library/kfhcywhs.aspx
  • 1. http://codebetter.com/blogs/raymond.lewallen/archive/2005/05/05/62809.aspx # of Methods Jitted – This counter displays the total number of methods compiled Just-In-Time (JIT) by the CLR JIT compiler since the start of the application. This counter does not include the pre-jitted methods.% Time in Jit – This counter displays the percentage of elapsed time spent in JIT compilation since the last JIT compilation phase. This counter is updated at the end of every JIT compilation phase. A JIT compilation phase is the phase when a method and its dependencies are being compiled.Standard Jit Failures – This counter displays the peak number of methods the JIT compiler has failed to JIT since the start of the application. This failure can occur if the IL cannot be verified or if there was an internal error in the JIT compiler.Total # of IL Bytes Jitted – This counter displays the total IL bytes jitted since the start of the application. This counter is exactly equivalent to the "# of IL Bytes Jitted" counter.2. JIT Overview: http://blogs.msdn.com/davidnotario/archive/2004/10/26/247792.aspx
  • http://msdn.microsoft.com/en-us/library/fxk122b4.aspxSystem performance counters are exposed in the Windows Performance monitor as the ASP.NET performance counter object.Application performance counters are exposed as the ASP.NET Applications performance object.
  • Things to look at:Fuslogvw.exe, Default and Native sections.How to enable fusion logging using registry:Set [HKLMSoftwareMicrosoftFusionLogPath] to point to some directory (like c:FusLog)Set [HKLMSoftwareMicrosoftFusionForceLog] as a DWORD value, to 1Fusion References:http://blogs.msdn.com/suzcook/archive/2003/05/29/57120.aspxhttp://msdn.microsoft.com/en-us/library/e74a18c4(VS.80).aspx
  • Useful Commands: 1. To display contents of Native assembly Cache NGEN display /verbose 2. To install assembly in NGEN cache NGEN install assemblyname 3. To uninstall assembly from NGEN cache NGEN uninstall assemblynameReferences: http://msdn.microsoft.com/en-us/library/6t9t5wcf(VS.80).aspxhttp://blogs.msdn.com/clrcodegeneration/
  • How to load SoS.dll in VSIn order to load sos.dll you have to open up the Immediate Window (Debug/Windows/Immediate or Ctrl+D, I) and type .load C:WINDOWSMicrosoft.NETFrameworkv2.0.50727sos.dll2. This should yield the response extension C:WINDOWSMicrosoft.NETFrameworkv2.0.50727sos.dll loaded2. References:http://blogs.msdn.com/tess/archive/2007/10/19/net-finalizer-memory-leak-debugging-with-sos-dll-in-visual-studio.aspxhttp://msdn.microsoft.com/en-us/library/bb190764.aspxhttp://blogs.msdn.com/vancem/archive/2006/03/07/545596.aspx
  • Enable MDA by adding following registry: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFramework] "MDA"="1"2. Add App.exe.mda.config file:<?xml version="1.0" encoding="UTF-8" ?> <mdaConfig> <assistants> <pInvokeStackImbalance /> </assistants> </mdaConfig>3. References:http://blogs.msdn.com/jmstall/archive/2005/11/10/introducing_mdas.aspx http://msdn.microsoft.com/en-us/library/d21c150d.aspxhttp://pinvoke.net
  • .Net Performance by Bijoy Singhal

    1. 1. .NetPerformance – Tips and Techniques<br />Bijoy Singhal<br />Developer Evangelist<br />Microsoft India<br />bsinghal@microsoft.com<br />
    2. 2. Key Take Aways…<br />Understanding reasons behind common issues that a .NET developer faces day to day<br />How to leverage the tools in .NET framework to solve day to day issues…<br />Performance: What and How…<br />MOST IMPORTANT: The POWER of .NET framework…<br />
    3. 3. .NET Tools…the hidden gems<br />FusLogVW.exe – Fusion Log Viewer<br />NGEN.exe – Native Image Generator<br />ILAsm.exe, ILDAsm.exe– IL Assembler and Dis-assembler<br />MDA – Managed Debugging Assistants <br />SoS.dll - <br />
    4. 4. Perfmon: What is it?<br />The name says it all.<br />Used to monitor the performance parameters of the system and the user applications.<br />How to use it?<br />What are the parameters that we can track?<br />
    5. 5. A few .NET Performance Counters<br />.NET CLR Memory Counters<br />.NET CLR Exceptions<br />.NET CLR Loading<br />.NET CLR JIT<br />Processor<br />Memory<br />
    6. 6. Scenario I: Application Consuming lot of memory<br />What should I look at?<br />Memory counters list<br />Indicators: Memory Leak?<br /># bytes in all Heaps increasing <br />Gen 2 Heap Size increasing <br /># GC handles increasing <br /># total committed/Reserved Bytes increasing<br />% Time in GC<br />Indicators: Memory Fragmentation?<br /># total reserved Bytes significantly larger than # total committed Bytes<br /># of Pinned Objects increasing <br /># GC handles increasing <br /># bytes in all heaps always increasing. <br />
    7. 7. Scenario II: Application throwing lot of exceptions unnecessarily…<br />Cost of throwing an exception<br />What else can go wrong?<br />Blocking exception handlers<br />Loss of exception context<br />Perfmon Exception counters to look at:<br /># of Exceps Thrown<br /># of Exceps Thrown / Sec<br /># of Filters / Sec<br />Throw to Catch Depth / Sec<br />
    8. 8. Scenario III: Application Takes too long to Load<br />Possible Reasons:<br />Too many assemblies to load<br />Too much of jitting happening.<br />Perfmon counters to look at:<br />% Time in Loading<br />Current Assemblies<br />Rate of Assemblies/AppDomains<br /># of IL Bytes JITted<br /># of methods JITed<br />% time in JIT<br />
    9. 9. A Quick note on ASP.NET specific counters<br />Two types:<br />System Counters<br />Application Counters<br />Few important Counters:<br />ASP.NET/Application Restarts.<br />ASP.NET/Requests Queued<br />ASP.NET Application/Requests/sec<br />ASP.NET Application/Errors Total<br />AND many more…<br />
    10. 10. .NET Application Performance<br />Performance is a relative term:<br />What is good for you may not be good for others.<br />Performance Bottlenecks in .NET:<br />Am I loading too many assemblies at the start?<br />Am I doing too many memory operations? Am I trying to deal with the memory management on my own?<br />Am I misusing Exceptions and defeating their purpose?<br />The list is long…<br />
    11. 11. .NET Application Performance<br />Way outs:<br />Add trace messages in your application.<br />Debug it.<br />Use Debugging tools to do post mortem analysis.<br />Use Perfmon.<br />
    12. 12. Scenario I: FileNotFoundException<br />“Our application ran successfully on the Development machine but it is failing with FileNotFoundException when we deploy it in production”<br />Fusion – How the CLR locates assemblies.<br />How can the fusion log viewer help us here<br />How to look into the Fusion Log<br />Fusion Log Viewer: Fuslogvw.exe<br />Demo<br />
    13. 13. Scenario II: Slow To Load<br />“Our application takes too long to load for the first time. It exhibits the same behavior once in a while…”<br />What happens when a .NET application runs for the first time<br />Native Images and how they speed up the loading process<br />NGEN: Native image generation<br />How NGEN will help us here<br />Demo<br />
    14. 14. Scenario III: Memory Leak<br />“ My application is consuming lot of memory. It looks like a ‘Memory Leak’ to me…”<br />.NET Memory Manager: GC<br />How to get managed heap information<br />How the CLRProfiler can help us here<br />Demo<br />
    15. 15. Scenario IV: PInvoke Issues<br />“ I used PInvoke to call a native API, I’m doing everything correct, but the call is failing/ the application is failing with AccessViolationException…”<br />Crucial to match the native API signature exactly without any exception<br />What is MDA<br />How MDA will help us here<br />Demo<br />
    16. 16. Thank You<br />Questions<br />???<br />Contact me: <br />bsinghal@microsoft.com<br />http://blogs.msdn.com/bsinghal<br />