Upload
sorin-oboroceanu
View
372
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Performance in .NET: Best practicesSorin Oboroceanu, [email protected] Balan, [email protected], www.rms.ro
Iași, 8th of May 2010
Agenda Why performance matters Serialization Reading XML Garbage Collection JITing
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
Why performance mattersBetter apps – Satisfied customers
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
Collections Groupping data
List<T> LINQ to Objects Dictionary<T,V>
DEMO
Client-Server communication Retrieving all users on the client Grouping data
List<T> LINQ to Objects Dictionary<T,V>
DEMO
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
Reading XML DataSet XmlReader LINQ to XML XmlDocument
DEMO
Garbage Collection Why memory matters Garbage Collector Object Finalization
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
.NET Memory Management Small Object Heap (SOH) – Objects < 85K Large Object Heap (LOH) – Objects => 85K
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
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
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)
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
Finalization public class Test {
~Test() {
Cleanup (); } private void Cleanup() { // clean up unmanaged resources } }
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);
} }
DEMO
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.}
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”);}
DEMO
Resources CLR via C# 3, Jeffrey Richter www.red-gate.com www.stackoverflow.com
Q&A
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