Performance in .net best practices

1,103 views

Published on

Performance in .NET: Best practices
Vlad Balan, Sorin Oboroceanu
www.rms.ro

  • Be the first to comment

  • Be the first to like this

Performance in .net best practices

  1. 1. Performance in .NET: Best practices<br />Sorin Oboroceanu, obs@rms.ro<br />Vlad Balan, bav@rms.ro<br />RomSoft, www.rms.ro<br />Iași, 8th of May 2010<br />
  2. 2. Agenda<br />Why performance matters<br />Serialization<br />Reading XML<br />Garbage Collection<br />JITing<br />
  3. 3. Why performance matters<br />Everyone loves performance<br />Performance=money<br />Bing - 2 seconds slower – 4.3% drop in revenue<br />Google – 400ms slower – 0.6% drop in searches<br />Yahoo! – 400ms slower – 5-9% drop in full page traffic<br />Mozilla – 2.2 seconds faster – increased download conversion 15.4%<br />Site speed is now taken into account in search rankings<br />
  4. 4. Why performance matters<br />Better apps – Satisfied customers<br />
  5. 5. Our Demo APP<br />Uses StackOverflow.com’s data<br />Groups users by location<br />Displays user locations in a chart<br />Will work in a client/server architecture<br />Has performance issues <br />
  6. 6. Collections<br />Groupping data<br />List<T><br />LINQ to Objects<br />Dictionary<T,V><br />DEMO<br />
  7. 7. Client-Server communication<br />Retrieving all users on the client<br />Grouping data<br />List<T><br />LINQ to Objects<br />Dictionary<T,V><br />DEMO<br />
  8. 8. Client-Server communication<br />Grouping data on the server<br />List<T><br />LINQ to Objects<br />Dictionary<T,V><br />Retrieving only location-related data on the client<br />DEMO<br />
  9. 9. Reading XML<br />DataSet<br />XmlReader<br />LINQ to XML<br />XmlDocument<br />DEMO<br />
  10. 10. Garbage Collection<br />Why memory matters<br />Garbage Collector<br />Object Finalization<br />
  11. 11. Why memory matters<br />Inefficient use of memory can impact<br />Performance<br />Stability<br />Scalability<br />Other Applications<br />Hidden problems in code can cause<br />Memory Leaks<br />Excessive memory usage<br />Unnecessary performance overhead<br />
  12. 12. .NET Memory Management<br />Small Object Heap (SOH) – Objects < 85K<br />Large Object Heap (LOH) – Objects => 85K<br />
  13. 13. Small Object Heap (SOH)<br />GC<br />SmallObjectObjectA = new SmallObject(); <br />SmallObjectObjectB = new SmallObject(); <br />Global Objects<br />Small Object Heap<br />Next Object Pointer<br />ObjectE<br />Next Object Pointer<br />Stack<br />ObjectD<br />Static Objects<br />Next Object Pointer<br />ObjectC<br />Next Object Pointer<br />ObjectB<br />ObjectB<br />Next Object Pointer<br />ObjectA<br />Root Reference<br />ObjectA<br />Next Object Pointer<br />Child Reference<br />
  14. 14. Generations<br />GC - Gen 0<br />GC - Gen 1<br />GC - Gen 2<br />Small Object Heap<br />Gen 0<br />Next Object Pointer<br />Global Objects<br />ObjectD<br />ObjectC<br />Next Object Pointer<br />Gen 1<br />Static Objects<br />Stack<br />ObjectB<br />Gen 2<br />Root Reference<br />ObjectA<br />
  15. 15. Large Object Heap (LOH)<br />GC- Gen2<br />LargeObjectObjectD= new LargeObject(); <br />Global Objects<br />Free space table<br />Large Object Heap<br />5727400<br />425000<br />16777216<br />ObjectD<br />94208<br />182272<br />Free space<br />Stack<br />ObjectC<br />ObjectC<br />ObjectB<br />Free space<br />ObjectB<br />ObjectA<br />ObjectA<br />
  16. 16. Finalization<br />GC<br />Finalization Queue<br />FinObjectObjectE = new FinObject(); <br />Small Object Heap<br />Gen 0<br />Global Objects<br />ObjectE<br />ObjectE<br />Finalize()<br />ObjectD<br />fReachable Queue<br />Finalizer thread<br />ObjectC<br />Stack<br />Gen 1<br />ObjectB<br />ObjectB<br />ObjectA<br />ObjectA<br />
  17. 17. Finalization<br />public class Test { ~Test() { Cleanup (); } private void Cleanup() {// clean up unmanaged resources } }<br />
  18. 18. Disposable pattern<br />public 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); } }<br />
  19. 19. DEMO<br />
  20. 20. JITing<br />Console<br />static void WriteLine();<br />static void WriteLine(string);<br />(remaining members)<br />Managed EXE<br />static void Main(){<br />Console.WriteLine(“Hello”);<br />Console.WriteLine(“GoodBye”);<br />}<br />JITCompiler<br />JITCompiler<br />MSCorEE.dll<br />…<br />JITCompiler function{<br />Look up the called method in the metadata<br />Get the IL for it from metadata<br />Allocate memory<br />Compile the IL into allocated memory<br />Modify the method’s entry in the Type’s table so it points to allocated memory<br />Jump to the native code contained inside the memory block.<br />}<br />Native CPU instr.<br />
  21. 21. JITing<br />Console<br />static void WriteLine();<br />static void WriteLine(string);<br />(remaining members)<br />Managed EXE<br />static void Main(){<br />Console.WriteLine(“Hello”);<br />Console.WriteLine(“GoodBye”);<br />}<br />JITCompiler<br />Native<br />MSCorEE.dll<br />…<br />JITCompiler function{<br />Lookup the called method in the metadata<br />Get the IL for it from metadata<br />Allocate memory<br />Compile the IL into allocated memory<br />Modify the method’s entry in the Type’s table so it points to allocated memory<br />Jump to the native code contained inside the memory block.}<br />Native CPU instr.<br />
  22. 22. DEMO<br />
  23. 23. Resources<br />CLR via C# 3, Jeffrey Richter<br />www.red-gate.com<br />www.stackoverflow.com<br />
  24. 24. Q&A<br />
  25. 25. Please fill the evaluation formThank you very much!<br />Sorin Oboroceanu, obs@rms.ro<br />Vlad Balan, bav@rms.ro<br />RomSoft, www.rms.ro<br />Iași, 8th of May 2010<br />

×