performance in .net best practices
DESCRIPTION
Performance in .NET: Best practicesVlad Balan, Sorin Oboroceanuwww.rms.roTRANSCRIPT
![Page 1: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/1.jpg)
Performance in .NET: Best practicesSorin Oboroceanu, [email protected] Balan, [email protected], www.rms.ro
Iași, 8th of May 2010
![Page 2: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/2.jpg)
Agenda Why performance matters Serialization Reading XML Garbage Collection JITing
![Page 3: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/3.jpg)
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
![Page 4: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/4.jpg)
Why performance mattersBetter apps – Satisfied customers
![Page 5: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/5.jpg)
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
![Page 6: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/6.jpg)
Collections Groupping data
List<T> LINQ to Objects Dictionary<T,V>
DEMO
![Page 7: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/7.jpg)
Client-Server communication Retrieving all users on the client Grouping data
List<T> LINQ to Objects Dictionary<T,V>
DEMO
![Page 8: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/8.jpg)
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
![Page 9: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/9.jpg)
Reading XML DataSet XmlReader LINQ to XML XmlDocument
DEMO
![Page 10: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/10.jpg)
Garbage Collection Why memory matters Garbage Collector Object Finalization
![Page 11: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/11.jpg)
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
![Page 12: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/12.jpg)
.NET Memory Management Small Object Heap (SOH) – Objects < 85K Large Object Heap (LOH) – Objects => 85K
![Page 13: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/13.jpg)
Small Object Heap (SOH)
Small Object Heap
Stack
SmallObject ObjectA = new SmallObject();
ObjectAObjectA Root Reference
SmallObject ObjectB = new SmallObject();
ObjectBObjectB
ObjectC
Child Reference
Global Objects
ObjectD
Next Object Pointer
ObjectE
Static Objects
GC
Next Object Pointer
Next Object Pointer
Next Object Pointer
Next Object Pointer
Next Object Pointer
![Page 14: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/14.jpg)
Small Object Heap
Gen 0
Gen 1
Gen 2
Stack
ObjectARoot Reference
ObjectB
ObjectC
Global Objects
ObjectD
Static Objects
GC - Gen 0
Next Object Pointer
Next Object Pointer
GC - Gen 1GC - Gen 2
Generations
![Page 15: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/15.jpg)
Large Object Heap
Stack
ObjectAObjectA
LargeObject ObjectD= new LargeObject();
ObjectBObjectB
ObjectC
Global Objects
Free space
GC- Gen2
ObjectC
From To
Free space table
425000 16777216
94208 182272
Free space
ObjectD5727400
Large Object Heap (LOH)
![Page 16: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/16.jpg)
Small Object Heap
Gen 0
Gen 1
Stack
ObjectAObjectA
FinObject ObjectE = new FinObject();
ObjectBObjectB
ObjectC
Global Objects
ObjectD
ObjectE
GC
Finalization Queue
fReachable Queue
Finalize() ObjectE
Finalizer thread
Finalization
![Page 17: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/17.jpg)
Finalization public class Test {
~Test() {
Cleanup (); } private void Cleanup() { // clean up unmanaged resources } }
![Page 18: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/18.jpg)
Disposable pattern public class Test : IDisposable{
~Test() {
Cleanup (false); } private void Cleanup(bool codeDispose) { if (codeDispose) {
// dispose managed resources }
// clean up unmanaged resources } public void Dispose() {
Cleanup (true); GC.SuppressFinalize(this);
} }
![Page 19: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/19.jpg)
DEMO
![Page 20: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/20.jpg)
JITingManaged EXE
static void Main(){ Console.WriteLine(“Hello”); Console.WriteLine(“GoodBye”);}
Console
static void WriteLine();
static void WriteLine(string);
(remaining members)
JITCompiler
JITCompiler
…
Native CPU instr.
MSCorEE.dll
JITCompiler function{1. Look up the called method in the metadata2. Get the IL for it from metadata3. Allocate memory4. Compile the IL into allocated memory5. Modify the method’s entry in the Type’s
table so it points to allocated memory6. Jump to the native code contained inside
the memory block.}
![Page 21: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/21.jpg)
JITingManaged EXE
Console
static void WriteLine();
static void WriteLine(string);
(remaining members)
JITCompiler
Native
…
MSCorEE.dll
JITCompiler function{1. Lookup the called method in the metadata2. Get the IL for it from metadata3. Allocate memory4. Compile the IL into allocated memory5. Modify the method’s entry in the Type’s
table so it points to allocated memory6. Jump to the native code contained inside
the memory block.}
Native CPU instr.
static void Main(){ Console.WriteLine(“Hello”); Console.WriteLine(“GoodBye”);}
![Page 22: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/22.jpg)
DEMO
![Page 23: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/23.jpg)
Resources CLR via C# 3, Jeffrey Richter www.red-gate.com www.stackoverflow.com
![Page 24: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/24.jpg)
Q&A
![Page 25: Performance in .net best practices](https://reader033.vdocuments.net/reader033/viewer/2022061220/54881e17b4af9f6e0d8b561a/html5/thumbnails/25.jpg)
Please fill the evaluation form
Thank you very much!Sorin Oboroceanu, [email protected] Balan, [email protected], www.rms.ro
Iași, 8th of May 2010