4. 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 4
7. Client-Server communication Grouping data on the server List<T> LINQ to Objects Dictionary<T,V> Retrieving only location-related data on the client 7 DEMO
10. 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 10
11. GC – Small Object Heap (SOH) 11 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 ObjectA Root Reference Next Object Pointer Child Reference
12. GC- Gen2 LargeObjectObjectD= new LargeObject(); Global Objects Free space table Large Object Heap 425000 16777216 ObjectD 5727400 94208 182272 Free space Stack ObjectC ObjectC ObjectB Free space ObjectB ObjectA ObjectA 12 12
13. 13 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 ObjectA Root Reference
14. GC – Minimizing Overhead 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); } } 14
15. GC – Common Memory Issues Excessive RAM Footprint App allocates objects too early or for too long using more memory than needed Can affect other apps on the system Excessive Temporary Object allocation Garbage Collection runs more frequently Executing threads freeze during Garbage Collection Memory Leaks Overlooked root references keep objects alive (Collections, array, session state, delegates/events) Incorrect or absent Finalization can cause resources leaks 15
17. JITing 17 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.
18. JITing 18 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.