deep dumpster diving 2010

Download Deep dumpster diving 2010

Post on 02-Jun-2015

481 views

Category:

Technology

4 download

Embed Size (px)

TRANSCRIPT

  • 1. Ronn Black
    October 2010
    Deep Dumpster DivingA close look at .Net garbage collection

2. Why should I care?
3. Demo 1 (Word Count)
using System;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
using System.IO;
publicclassMyClass
{
publicstaticvoidRunSnippet()
{
while(true)
{
Stopwatch watch = new Stopwatch();
watch.Start();
StreamReadersr = newStreamReader(@"C:UsersRonnDocumentsMy Code SnippetsGarbage Collectioncatcher.txt");
string text = sr.ReadToEnd();
intwordCount = text.Split().Length;
Console.WriteLine("{0} Words", wordCount);
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds + " Milliseconds");
}
}
4. 5. using System;
usingSystem.Collections.Generic;
usingSystem.Threading;
usingSystem.Runtime.CompilerServices;
publicclassMyClass
{
static Byte[] bytes;
publicstaticvoidRunSnippet()
{
Timer tmr = new Timer(M, null, 0, 1000);
Thread.Sleep(20);
for(int i = 0; i < 1000; i++)
bytes = new Byte[2000];
Console.ReadLine();
}
staticvoid M(object state)
{
Console.WriteLine("M - " + DateTime.Now);
}
Demo 2
6. Unmanaged Memory Management
7. Hi Address
Unused Area
Unused Area
Args & Variables
Stack
Stack Pointer
Heap
Reserved
Low Address
8. 9. 10. 11. 12. NextObjPtr
13. NextObjPtr
14. Roots
NextObjPtr
15. NextObjPtr
Roots
16. Finalizers
=
~MyClass(){//Do work here}
MyClass.Finalize(){//Do work here}
17. Roots
Finalization Queue
I
I
H
F
G
E
F
C
E
Freachable Queue
D
C
B
A
18. Roots
Finalization Queue
I (x)
I (x)
H (x)
F
G (x)
E (x)
F
C
E (x)
Freachable Queue
D
C
B (x)
A
19. Roots
Finalization Queue
F
I (x)
C
F
Freachable Queue
E (x)
D
C
I (x)
A
E (x)
20. Optimizations
Generations
Newly created objects tend to have short lives.
The older an object is, the longer it will survive.
Groups objects by age and collects younger objects more frequently than older objects.
All objects added to heap are in generation 0.
When an object survives the first garbage collection it is promoted to generation 1.
When garbage collection is triggered survivors from generation 1 are promoted to generation 2 and generation 0 survivors are promoted to gen 1.
As objects "mature", they are moved to the next older generation until they reach gen 2.
21. using System;
usingSystem.Collections.Generic;
usingSystem.Threading;
usingSystem.Runtime.CompilerServices;
publicclassMyClass
{
static Byte[] bytes;
publicstaticvoidRunSnippet()
{
Byte[] bytes;
Timer tmr = new Timer(M, null, 0, 1000);
Thread.Sleep(20);
for(int i = 0; i < 1000; i++)
bytes = new Byte[2000];
Console.ReadLine();
}
staticvoid M(object state)
{
Console.WriteLine("M - " + DateTime.Now);
}
Demo 3 WTF??
22. using System;
usingSystem.Collections.Generic;
usingSystem.Threading;
usingSystem.Runtime.CompilerServices;
publicclassMyClass
{
static Byte[] bytes;
publicstaticvoidRunSnippet()
{
Byte[] bytes;
Timer tmr = new Timer(M, null, 0, 1000);
Thread.Sleep(20);
for(int i = 0; i < 1000; i++)
bytes = new Byte[2000];
Console.WriteLine(bytes.Length);
Console.ReadLine();
}
staticvoid M(object state)
{
Console.WriteLine("M - " + DateTime.Now);
}
Demo 3 WTF??
23. Demo 3 WTF??
using System;
usingSystem.Collections.Generic;
usingSystem.Threading;
usingSystem.Runtime.CompilerServices;
publicclassMyClass
{
static Byte[] bytes;
publicstaticvoidRunSnippet()
{
Byte[] bytes;
Timer tmr = new Timer(M, null, 0, 1000);
Thread.Sleep(20);
for(int i = 0; i < 1000; i++)
{
bytes = new Byte[2000];
Console.WriteLine(XX);
}
Console.ReadLine();
}
staticvoid M(object state)
{
Console.WriteLine("M - " + DateTime.Now);
}
24. Demo 4 (CLR Profile Word Count)
using System;
usingSystem.Collections.Generic;
usingSystem.Diagnostics;
using System.IO;
publicclassMyClass
{
publicstaticvoidRunSnippet()
{
while(true)
{
Stopwatch watch = new Stopwatch();
watch.Start();
StreamReadersr = newStreamReader(@"C:UsersRonnDocumentsMy Code SnippetsGarbage Collectioncatcher.txt");
string text = sr.ReadToEnd();
intwordCount = text.Split().Length;
Console.WriteLine("{0} Words", wordCount);
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds + " Milliseconds");
}
}
25. 26. 27. 28. 29. 30. 31. IDisposable
public class MyClass : IDisposable
{
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
// Dispose managed resources. Ex:Components.Dispose();
}
// Release ONLY unmanaged resources. Ex:CloseHandle(handle);
}
disposed = true;
}
protected volatile bool disposed = false;
~MyClass()
{
Dispose(false);
}
}
[ComVisible(true)]
public interface IDisposable
{
void Dispose();
}
32. Using
using System;
using System.Collections.Generic;
using System.Diagnostics;
33. Using
using System;
using System.Collections.Generic;
using System.Diagnostics;
using (MyClass c = new MyClass())
{
//Do Some Work
}
34. Demo 5 - optimize
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
public class MyClass
{
public static void RunSnippet()
{
while(true)
{
Stopwatch watch = new Stopwatch();
watch.Start();
using(StreamReadersr = new StreamReader(@"C:Garbage Collectioncatcher.txt"))
{
string line = "";
intwordCount = 0;
while((line = sr.ReadLine()) != null)
{
wordCount += line.Split().Length;
}
Console.WriteLine("{0} Words", wordCount);
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds + " Milliseconds");
}
}
35. 36. TotalRelocatedFinalGen 0 Gen 1Large Object Heap6,578,03896,6085,057,2721,400,580123,535,4645,473,972101,5011,441,2012,097,172103,9929,328====================================================-1,104,066+4,893-3,617,071+696,592+103,980-3,526,136
37. Design patterns and Memory
38. Design patterns and Memory
Circular References.
Form
Control
Control
Control
Control
+Controls
+Parent
+Parent
+Parent
+Parent
39. Design patterns and Memory
public class Preferences
{
public Preferences instance;
public static Preferences GetPrefs()
{
if (instance == null)
instance = new Preferences();
return instance;
}
public event PrefsChanged;
}
40. Design patterns and Memory
Rooted objects (Singletons).
Form
Control
Control
Control
Control
Preferences
+Controls
+Parent
+Parent
+Parent
+Parent
$GetPrefs
+PrefsChanged
41. Design patterns and Memory
Lists, Hashtables, Dictionaries, etc.
Control
Control
Control
T
List
+Parent
+Parent
+Parent

42. Design patterns and Memory
public class Foo
{
public static void DoSomething()
{
List bars;
...
//Do Something
bar.Clear();
}
}
43. Design patterns and Memory
public class Foo
{
static Dictionary _bars;
public static Foo()
{
//Initialize the Lookup table
_bars = new Dictionary();
_bars.Add(EndUp, new Bar());
...
}
}
44. Take Aways
Dont keep objects around unless you know you will be using them again.
Save these techniques for objects that are expensive to create and are frequently used.
Carefully consider use of type initializers and statics.
Consider caching patterns so memory can be reclaimed if needed.
If you are using observable patterns be sure you unsubscribe properly
45. Contact & Reference Material
Ronn Blackrblack@btsoft.org
http://msdn.microsoft.com/en-us/library/ms973837.aspx (Garbage Collector Basics and Performance Hints)
http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&DisplayLang=en (CLR Profiler for .Net 2.0)
http://www.openasthra.com/multithreading/heap-overview/ (Heap Overview)
http://74.125.155.132/search?q=cache:44hDjSztDf4J:doc.bughunter.net/buffer-overflow/advanced-malloc-exploits.html+malloc+overview&cd=21&hl=en&ct=clnk&gl=us Advanced Malloc exploits
http://msdn.microsoft.com/en-us/magazine/cc534993.aspx (Large Object Heap Uncovered)
http://msdn.microsoft.com/en-us/library/aa970850.aspx (Weak Event Patterns)