performance in the .net world
TRANSCRIPT
![Page 1: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/2.jpg)
Agenda
String vs. StringBuilder Serialization Reading XML Garbage Collection JITing
2
![Page 3: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/3.jpg)
String vs. StringBuilder
3
DEMO
![Page 4: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/4.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
4
![Page 5: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/5.jpg)
Collections
Groupping data List<T> LINQ to Objects Dictionary<T,V>
5
DEMO
![Page 6: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/7.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
7
DEMO
![Page 8: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/8.jpg)
Reading XML
DataSet XmlReader LINQ to XML XmlDocument
8
DEMO
![Page 9: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/9.jpg)
Garbage Collection
Why memory matters Garbage Collector Common Memory Issues Diagnosing Memory Problems
9
![Page 10: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/10.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
10
![Page 11: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/16.jpg)
DEMO
16
![Page 17: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/19.jpg)
DEMO
19
![Page 20: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/20.jpg)
Resources
CLR via C# 3, Jeffrey Richter www.red-gate.com www.stackoverflow.com MSDN
20
![Page 21: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/21.jpg)
Q&A
21
![Page 22: Performance In The .Net World](https://reader035.vdocuments.us/reader035/viewer/2022062405/556cca0cd8b42aba548b5085/html5/thumbnails/22.jpg)
22
Please fill the evaluation form
Thank you very much!
Sorin Oboroceanu, Vlad Balan
RomSoft, www.rms.ro
Iasi, May 4th 2010