3. Why performance matters Everyone loves performance Performance=money Bing - 2 seconds slower – 4.3% drop in revenue Google – 400ms slower – 0.6% drop in searches Yahoo! – 400ms slower – 5-9% drop in full page traffic Mozilla – 2.2 seconds faster – increased download conversion 15.4% Site speed is now taken into account in search rankings
5. Our Demo APP Uses StackOverflow.com’s data Groups users by location Displays user locations in a chart Will work in a client/server architecture Has performance issues
8. Client-Server communication Grouping data on the server List<T> LINQ to Objects Dictionary<T,V> Retrieving only location-related data on the client DEMO
11. Why memory matters Inefficient use of memory can impact Performance Stability Scalability Other Applications Hidden problems in code can cause Memory Leaks Excessive memory usage Unnecessary performance overhead
12. .NET Memory Management Small Object Heap (SOH) – Objects < 85K Large Object Heap (LOH) – Objects => 85K
13. Small Object Heap (SOH) GC SmallObjectObjectA = new SmallObject(); SmallObjectObjectB = new SmallObject(); Global Objects Small Object Heap Next Object Pointer ObjectE Next Object Pointer Stack ObjectD Static Objects Next Object Pointer ObjectC Next Object Pointer ObjectB ObjectB Next Object Pointer ObjectA Root Reference ObjectA Next Object Pointer Child Reference
14. Generations GC - Gen 0 GC - Gen 1 GC - Gen 2 Small Object Heap Gen 0 Next Object Pointer Global Objects ObjectD ObjectC Next Object Pointer Gen 1 Static Objects Stack ObjectB Gen 2 Root Reference ObjectA
15. Large Object Heap (LOH) GC- Gen2 LargeObjectObjectD= new LargeObject(); Global Objects Free space table Large Object Heap 5727400 425000 16777216 ObjectD 94208 182272 Free space Stack ObjectC ObjectC ObjectB Free space ObjectB ObjectA ObjectA
16. Finalization GC Finalization Queue FinObjectObjectE = new FinObject(); Small Object Heap Gen 0 Global Objects ObjectE ObjectE Finalize() ObjectD fReachable Queue Finalizer thread ObjectC Stack Gen 1 ObjectB ObjectB ObjectA ObjectA
17. Finalization public class Test { ~Test() { Cleanup (); } private void Cleanup() {// clean up unmanaged resources } }
18. Disposable pattern 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); } }
20. JITing Console static void WriteLine(); static void WriteLine(string); (remaining members) Managed EXE static void Main(){ Console.WriteLine(“Hello”); Console.WriteLine(“GoodBye”); } JITCompiler JITCompiler MSCorEE.dll … JITCompiler function{ Look up the called method in the metadata Get the IL for it from metadata Allocate memory Compile the IL into allocated memory Modify the method’s entry in the Type’s table so it points to allocated memory Jump to the native code contained inside the memory block. } Native CPU instr.
21. JITing Console static void WriteLine(); static void WriteLine(string); (remaining members) Managed EXE static void Main(){ Console.WriteLine(“Hello”); Console.WriteLine(“GoodBye”); } JITCompiler Native MSCorEE.dll … JITCompiler function{ Lookup the called method in the metadata Get the IL for it from metadata Allocate memory Compile the IL into allocated memory Modify the method’s entry in the Type’s table so it points to allocated memory Jump to the native code contained inside the memory block.} Native CPU instr.