25
Performance in .NET: Best practices Sorin Oboroceanu, [email protected] Vlad Balan, [email protected] RomSoft, www.rms.ro Iași, 8 th of May 2010

Performance in .NET - Best practices

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Performance in .NET - Best practices

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

Agenda Why performance matters Serialization Reading XML Garbage Collection JITing

Page 3: Performance in .NET - Best practices

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

Why performance mattersBetter apps – Satisfied customers

Page 5: Performance in .NET - Best practices

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

Collections Groupping data

List<T> LINQ to Objects Dictionary<T,V>

DEMO

Page 7: Performance in .NET - Best practices

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

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

Reading XML DataSet XmlReader LINQ to XML XmlDocument

DEMO

Page 10: Performance in .NET - Best practices

Garbage Collection Why memory matters Garbage Collector Object Finalization

Page 11: Performance in .NET - Best practices

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

.NET Memory Management Small Object Heap (SOH) – Objects < 85K Large Object Heap (LOH) – Objects => 85K

Page 13: Performance in .NET - Best practices

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

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

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

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

Finalization public class Test {

~Test() {

Cleanup (); } private void Cleanup() { // clean up unmanaged resources } }

Page 18: Performance in .NET - Best practices

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

DEMO

Page 20: Performance in .NET - Best practices

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

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

DEMO

Page 23: Performance in .NET - Best practices

Resources CLR via C# 3, Jeffrey Richter www.red-gate.com www.stackoverflow.com

Page 24: Performance in .NET - Best practices

Q&A

Page 25: Performance in .NET - Best practices

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