Performance in the .NET world. A developer’s perspective Sorin Oboroceanu, Vlad Balan RomSoft, www.rms.ro Iaşi, May 4 th 2010
Jun 02, 2015
Performance in the .NET world. A developer’s perspective
Sorin Oboroceanu, Vlad Balan
RomSoft, www.rms.ro
Iaşi, May 4th 2010
Agenda
String vs. StringBuilder Serialization Reading XML Garbage Collection JITing
2
String vs. StringBuilder
3
DEMO
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
Collections
Groupping data List<T> LINQ to Objects Dictionary<T,V>
5
DEMO
Client-Server communication
Retrieving all users on the client Grouping data
List<T> LINQ to Objects Dictionary<T,V>
6
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
7
DEMO
Reading XML
DataSet XmlReader LINQ to XML XmlDocument
8
DEMO
Garbage Collection
Why memory matters Garbage Collector Common Memory Issues Diagnosing Memory Problems
9
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
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
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
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
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
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
DEMO
16
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.}
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”);}
DEMO
19
Resources
CLR via C# 3, Jeffrey Richter www.red-gate.com www.stackoverflow.com MSDN
20
Q&A
21
22
Please fill the evaluation form
Thank you very much!
Sorin Oboroceanu, Vlad Balan
RomSoft, www.rms.ro
Iasi, May 4th 2010