performance in the .net world

22
Performance in the .NET world. A developer’s perspective Sorin Oboroceanu, Vlad Balan RomSoft, www.rms.ro Iaşi, May 4 th 2010

Upload: vlad-balan

Post on 02-Jun-2015

360 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Performance In The .Net World

Performance in the .NET world. A developer’s perspective

Sorin Oboroceanu, Vlad Balan

RomSoft, www.rms.ro

Iaşi, May 4th 2010

Page 2: Performance In The .Net World

Agenda

String vs. StringBuilder Serialization Reading XML Garbage Collection JITing

2

Page 3: Performance In The .Net World

String vs. StringBuilder

3

DEMO

Page 4: Performance In The .Net World

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

Page 5: Performance In The .Net World

Collections

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

5

DEMO

Page 6: Performance In The .Net World

Client-Server communication

Retrieving all users on the client Grouping data

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

6

DEMO

Page 7: Performance In The .Net World

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

Page 8: Performance In The .Net World

Reading XML

DataSet XmlReader LINQ to XML XmlDocument

8

DEMO

Page 9: Performance In The .Net World

Garbage Collection

Why memory matters Garbage Collector Common Memory Issues Diagnosing Memory Problems

9

Page 10: Performance In The .Net World

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

Page 11: Performance In The .Net World

GC – Small Object Heap (SOH)

11

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 12: Performance In The .Net World

1212

Large Object Heap

Stack

ObjectAObjectA

LargeObject ObjectD= new LargeObject();

ObjectBObjectB

ObjectC

Global Objects

Free space

GC- Gen2

ObjectC

From ToFree space table

425000 16777216

94208 182272

Free space

ObjectD5727400

Page 13: Performance In The .Net World

13

Small Object Heap

Gen 0

Gen 1

Gen 2

Stack

ObjectA Root Reference

ObjectB

ObjectC

Global Objects

ObjectD

Static Objects

GC - Gen 0

Next Object Pointer

Next Object Pointer

GC - Gen 1GC - Gen 2

Page 14: Performance In The .Net World

GC – Minimizing Overhead 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);

} }1

4

Page 15: Performance In The .Net World

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

leaks15

Page 16: Performance In The .Net World

DEMO

16

Page 17: Performance In The .Net World

JITing

17

Managed 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 18: Performance In The .Net World

JITing

18

Managed 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 19: Performance In The .Net World

DEMO

19

Page 20: Performance In The .Net World

Resources

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

20

Page 21: Performance In The .Net World

Q&A

21

Page 22: Performance In The .Net World

22

Please fill the evaluation form

Thank you very much!

Sorin Oboroceanu, Vlad Balan

RomSoft, www.rms.ro

Iasi, May 4th 2010