SlideShare a Scribd company logo
Performance in .NET: Best practicesSorin Oboroceanu, obs@rms.roVlad Balan, bav@rms.roRomSoft, www.rms.roIași, 8th of May 2010
AgendaWhy performance mattersSerializationReading XMLGarbage CollectionJITing
Why performance mattersEveryone loves performancePerformance=moneyBing  - 2 seconds slower – 4.3% drop in revenueGoogle – 400ms slower – 0.6% drop in searchesYahoo! – 400ms slower – 5-9% drop in full page trafficMozilla – 2.2 seconds faster – increased download conversion 15.4%Site speed is now taken into account in search rankings
Why performance mattersBetter apps – Satisfied customers
Our Demo APPUses StackOverflow.com’s dataGroups users by locationDisplays user locations in a chartWill work in a client/server architectureHas performance issues 
CollectionsGroupping dataList<T>LINQ to ObjectsDictionary<T,V>DEMO
Client-Server communicationRetrieving all users on the clientGrouping dataList<T>LINQ to ObjectsDictionary<T,V>DEMO
Client-Server communicationGrouping data on the serverList<T>LINQ to ObjectsDictionary<T,V>Retrieving only location-related data on the clientDEMO
Reading XMLDataSetXmlReaderLINQ to XMLXmlDocumentDEMO
Garbage CollectionWhy memory mattersGarbage CollectorObject Finalization
Why memory mattersInefficient use of memory can impactPerformanceStabilityScalabilityOther ApplicationsHidden problems in code can causeMemory LeaksExcessive memory usageUnnecessary performance overhead
.NET Memory ManagementSmall Object Heap (SOH) – Objects < 85KLarge Object Heap (LOH) – Objects => 85K
Small Object Heap (SOH)GCSmallObjectObjectA = new SmallObject(); SmallObjectObjectB = new SmallObject(); Global ObjectsSmall Object  HeapNext Object PointerObjectENext Object PointerStackObjectDStatic ObjectsNext Object PointerObjectCNext Object PointerObjectBObjectBNext Object PointerObjectARoot ReferenceObjectANext Object PointerChild Reference
GenerationsGC - Gen 0GC - Gen 1GC - Gen 2Small Object  HeapGen 0Next Object PointerGlobal ObjectsObjectDObjectCNext Object PointerGen 1Static ObjectsStackObjectBGen 2Root ReferenceObjectA
Large Object Heap (LOH)GC- Gen2LargeObjectObjectD= new LargeObject(); Global ObjectsFree space tableLarge Object  Heap572740042500016777216ObjectD94208182272Free spaceStackObjectCObjectCObjectBFree spaceObjectBObjectAObjectA
FinalizationGCFinalization QueueFinObjectObjectE = new FinObject(); Small Object  HeapGen 0Global ObjectsObjectEObjectEFinalize()ObjectDfReachable QueueFinalizer threadObjectCStackGen 1ObjectBObjectBObjectAObjectA
Finalizationpublic class Test {  ~Test()  {	Cleanup ();   }  private void Cleanup()  {// clean up unmanaged resources } }
Disposable patternpublic class Test : IDisposable{  ~Test()  {	Cleanup (false);   }  private void Cleanup(boolcodeDispose)  {      if (codeDispose)      {	// dispose managed resources        }	// clean up unmanaged resources   }  public void Dispose()  {	Cleanup (true); GC.SuppressFinalize(this);  } }
DEMO
JITingConsolestatic void WriteLine();static void WriteLine(string);(remaining members)Managed EXEstatic void Main(){Console.WriteLine(“Hello”);Console.WriteLine(“GoodBye”);}JITCompilerJITCompilerMSCorEE.dll…JITCompiler function{Look up the called method in the metadataGet the IL for it from metadataAllocate memoryCompile the IL into allocated memoryModify the method’s entry in the Type’s table so it points to allocated memoryJump to the native code contained inside the memory block.}Native CPU instr.
JITingConsolestatic void WriteLine();static void WriteLine(string);(remaining members)Managed EXEstatic void Main(){Console.WriteLine(“Hello”);Console.WriteLine(“GoodBye”);}JITCompilerNativeMSCorEE.dll…JITCompiler function{Lookup the called method in the metadataGet the IL for it from metadataAllocate memoryCompile the IL into allocated memoryModify the method’s entry in the Type’s table so it points to allocated memoryJump to the native code contained inside the memory block.}Native CPU instr.
DEMO
ResourcesCLR via C# 3, Jeffrey Richterwww.red-gate.comwww.stackoverflow.com
Q&A
Please fill the evaluation formThank you very much!Sorin Oboroceanu, obs@rms.roVlad Balan, bav@rms.roRomSoft, www.rms.roIași, 8th of May 2010

More Related Content

Performance in .net best practices

  • 1. Performance in .NET: Best practicesSorin Oboroceanu, obs@rms.roVlad Balan, bav@rms.roRomSoft, www.rms.roIași, 8th of May 2010
  • 3. Why performance mattersEveryone loves performancePerformance=moneyBing - 2 seconds slower – 4.3% drop in revenueGoogle – 400ms slower – 0.6% drop in searchesYahoo! – 400ms slower – 5-9% drop in full page trafficMozilla – 2.2 seconds faster – increased download conversion 15.4%Site speed is now taken into account in search rankings
  • 4. Why performance mattersBetter apps – Satisfied customers
  • 5. Our Demo APPUses StackOverflow.com’s dataGroups users by locationDisplays user locations in a chartWill work in a client/server architectureHas performance issues 
  • 6. CollectionsGroupping dataList<T>LINQ to ObjectsDictionary<T,V>DEMO
  • 7. Client-Server communicationRetrieving all users on the clientGrouping dataList<T>LINQ to ObjectsDictionary<T,V>DEMO
  • 8. Client-Server communicationGrouping data on the serverList<T>LINQ to ObjectsDictionary<T,V>Retrieving only location-related data on the clientDEMO
  • 10. Garbage CollectionWhy memory mattersGarbage CollectorObject Finalization
  • 11. Why memory mattersInefficient use of memory can impactPerformanceStabilityScalabilityOther ApplicationsHidden problems in code can causeMemory LeaksExcessive memory usageUnnecessary performance overhead
  • 12. .NET Memory ManagementSmall Object Heap (SOH) – Objects < 85KLarge Object Heap (LOH) – Objects => 85K
  • 13. Small Object Heap (SOH)GCSmallObjectObjectA = new SmallObject(); SmallObjectObjectB = new SmallObject(); Global ObjectsSmall Object HeapNext Object PointerObjectENext Object PointerStackObjectDStatic ObjectsNext Object PointerObjectCNext Object PointerObjectBObjectBNext Object PointerObjectARoot ReferenceObjectANext Object PointerChild Reference
  • 14. GenerationsGC - Gen 0GC - Gen 1GC - Gen 2Small Object HeapGen 0Next Object PointerGlobal ObjectsObjectDObjectCNext Object PointerGen 1Static ObjectsStackObjectBGen 2Root ReferenceObjectA
  • 15. Large Object Heap (LOH)GC- Gen2LargeObjectObjectD= new LargeObject(); Global ObjectsFree space tableLarge Object Heap572740042500016777216ObjectD94208182272Free spaceStackObjectCObjectCObjectBFree spaceObjectBObjectAObjectA
  • 16. FinalizationGCFinalization QueueFinObjectObjectE = new FinObject(); Small Object HeapGen 0Global ObjectsObjectEObjectEFinalize()ObjectDfReachable QueueFinalizer threadObjectCStackGen 1ObjectBObjectBObjectAObjectA
  • 17. Finalizationpublic class Test { ~Test() { Cleanup (); } private void Cleanup() {// clean up unmanaged resources } }
  • 18. Disposable patternpublic class Test : IDisposable{ ~Test() { Cleanup (false); } private void Cleanup(boolcodeDispose) { if (codeDispose) { // dispose managed resources } // clean up unmanaged resources } public void Dispose() { Cleanup (true); GC.SuppressFinalize(this); } }
  • 19. DEMO
  • 20. JITingConsolestatic void WriteLine();static void WriteLine(string);(remaining members)Managed EXEstatic void Main(){Console.WriteLine(“Hello”);Console.WriteLine(“GoodBye”);}JITCompilerJITCompilerMSCorEE.dll…JITCompiler function{Look up the called method in the metadataGet the IL for it from metadataAllocate memoryCompile the IL into allocated memoryModify the method’s entry in the Type’s table so it points to allocated memoryJump to the native code contained inside the memory block.}Native CPU instr.
  • 21. JITingConsolestatic void WriteLine();static void WriteLine(string);(remaining members)Managed EXEstatic void Main(){Console.WriteLine(“Hello”);Console.WriteLine(“GoodBye”);}JITCompilerNativeMSCorEE.dll…JITCompiler function{Lookup the called method in the metadataGet the IL for it from metadataAllocate memoryCompile the IL into allocated memoryModify the method’s entry in the Type’s table so it points to allocated memoryJump to the native code contained inside the memory block.}Native CPU instr.
  • 22. DEMO
  • 23. ResourcesCLR via C# 3, Jeffrey Richterwww.red-gate.comwww.stackoverflow.com
  • 24. Q&A
  • 25. Please fill the evaluation formThank you very much!Sorin Oboroceanu, obs@rms.roVlad Balan, bav@rms.roRomSoft, www.rms.roIași, 8th of May 2010