Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Deep dumpster diving 2010

695 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Deep dumpster diving 2010

  1. 1. Ronn Black<br />October 2010<br />Deep Dumpster DivingA close look at .Net garbage collection<br />
  2. 2. Why should I care?<br />
  3. 3. Demo 1 (Word Count)<br />using System;<br />usingSystem.Collections.Generic;<br />usingSystem.Diagnostics;<br />using System.IO;<br />publicclassMyClass<br />{<br /> publicstaticvoidRunSnippet()<br /> {<br /> while(true)<br /> {<br /> Stopwatch watch = new Stopwatch();<br />watch.Start();<br />StreamReadersr = newStreamReader(@"C:UsersRonnDocumentsMy Code SnippetsGarbage Collectioncatcher.txt");<br /> string text = sr.ReadToEnd();<br />intwordCount = text.Split().Length;<br />Console.WriteLine("{0} Words", wordCount);<br />watch.Stop();<br />Console.WriteLine(watch.ElapsedMilliseconds + " Milliseconds");<br /> }<br /> }<br />
  4. 4.
  5. 5. using System;<br />usingSystem.Collections.Generic;<br />usingSystem.Threading;<br />usingSystem.Runtime.CompilerServices;<br />publicclassMyClass<br />{<br /> static Byte[] bytes;<br /> publicstaticvoidRunSnippet()<br /> {<br /> Timer tmr = new Timer(M, null, 0, 1000);<br />Thread.Sleep(20);<br /> for(int i = 0; i < 1000; i++)<br /> bytes = new Byte[2000];<br />Console.ReadLine();<br /> }<br /> staticvoid M(object state)<br /> {<br />Console.WriteLine("M - " + DateTime.Now);<br /> }<br />Demo 2<br />
  6. 6. Unmanaged Memory Management <br />
  7. 7. Hi Address<br />Unused Area<br />Unused Area<br />Args & Variables<br />Stack<br />Stack Pointer<br />Heap<br />Reserved<br />Low Address<br />
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12. NextObjPtr<br />
  13. 13. NextObjPtr<br />
  14. 14. Roots<br />NextObjPtr<br />
  15. 15. NextObjPtr<br />Roots<br />
  16. 16. Finalizers<br />=<br />~MyClass(){ //Do work here…}<br />MyClass.Finalize(){ //Do work here…}<br />
  17. 17. Roots<br />Finalization Queue<br />I<br />I<br />H<br />F<br />G<br />E<br />F<br />C<br />E<br />Freachable Queue<br />D<br />C<br />B<br />A<br />
  18. 18. Roots<br />Finalization Queue<br />I (x)<br />I (x)<br />H (x)<br />F<br />G (x)<br />E (x)<br />F<br />C<br />E (x)<br />Freachable Queue<br />D<br />C<br />B (x)<br />A<br />
  19. 19. Roots<br />Finalization Queue<br />F<br />I (x)<br />C<br />F<br />Freachable Queue<br />E (x)<br />D<br />C<br />I (x)<br />A<br />E (x)<br />
  20. 20. Optimizations<br />Generations<br />Newly created objects tend to have short lives.<br />The older an object is, the longer it will survive. <br />Groups objects by age and collects younger objects more frequently than older objects.<br />All objects added to heap are in generation 0.<br />When an object survives the first garbage collection it is promoted to generation 1.<br />When garbage collection is triggered survivors from generation 1 are promoted to generation 2 and generation 0 survivors are promoted to gen 1.<br />As objects "mature", they are moved to the next older generation until they reach gen 2. <br />
  21. 21. using System;<br />usingSystem.Collections.Generic;<br />usingSystem.Threading;<br />usingSystem.Runtime.CompilerServices;<br />publicclassMyClass<br />{<br />static Byte[] bytes;<br /> publicstaticvoidRunSnippet()<br /> {<br />Byte[] bytes;<br /> Timer tmr = new Timer(M, null, 0, 1000);<br />Thread.Sleep(20);<br /> for(int i = 0; i < 1000; i++)<br /> bytes = new Byte[2000];<br />Console.ReadLine();<br /> }<br /> staticvoid M(object state)<br /> {<br />Console.WriteLine("M - " + DateTime.Now);<br /> }<br />Demo 3 – WTF??<br />
  22. 22. using System;<br />usingSystem.Collections.Generic;<br />usingSystem.Threading;<br />usingSystem.Runtime.CompilerServices;<br />publicclassMyClass<br />{<br />static Byte[] bytes;<br /> publicstaticvoidRunSnippet()<br /> {<br />Byte[] bytes;<br /> Timer tmr = new Timer(M, null, 0, 1000);<br />Thread.Sleep(20);<br /> for(int i = 0; i < 1000; i++)<br /> bytes = new Byte[2000];<br />Console.WriteLine(bytes.Length);<br />Console.ReadLine();<br /> }<br /> staticvoid M(object state)<br /> {<br />Console.WriteLine("M - " + DateTime.Now);<br /> }<br />Demo 3 – WTF??<br />
  23. 23. Demo 3 – WTF??<br />using System;<br />usingSystem.Collections.Generic;<br />usingSystem.Threading;<br />usingSystem.Runtime.CompilerServices;<br />publicclassMyClass<br />{<br />static Byte[] bytes;<br /> publicstaticvoidRunSnippet()<br /> {<br />Byte[] bytes;<br /> Timer tmr = new Timer(M, null, 0, 1000);<br />Thread.Sleep(20);<br /> for(int i = 0; i < 1000; i++)<br /> {<br /> bytes = new Byte[2000];<br />Console.WriteLine(“XX”);<br /> }<br />Console.ReadLine();<br /> }<br /> staticvoid M(object state)<br /> {<br />Console.WriteLine("M - " + DateTime.Now);<br /> }<br />
  24. 24. Demo 4 (CLR Profile Word Count)<br />using System;<br />usingSystem.Collections.Generic;<br />usingSystem.Diagnostics;<br />using System.IO;<br />publicclassMyClass<br />{<br /> publicstaticvoidRunSnippet()<br /> {<br /> while(true)<br /> {<br /> Stopwatch watch = new Stopwatch();<br />watch.Start();<br />StreamReadersr = newStreamReader(@"C:UsersRonnDocumentsMy Code SnippetsGarbage Collectioncatcher.txt");<br /> string text = sr.ReadToEnd();<br />intwordCount = text.Split().Length;<br />Console.WriteLine("{0} Words", wordCount);<br />watch.Stop();<br />Console.WriteLine(watch.ElapsedMilliseconds + " Milliseconds");<br /> }<br /> }<br />
  25. 25.
  26. 26.
  27. 27.
  28. 28.
  29. 29.
  30. 30.
  31. 31. IDisposable<br />public class MyClass : IDisposable<br />{<br />public void Dispose()<br /> {<br /> Dispose(true);<br />GC.SuppressFinalize(this);<br /> }<br />protected virtual void Dispose(bool disposing)<br /> {<br />if (!disposed)<br /> {<br />if (disposing)<br /> {<br />// Dispose managed resources. Ex: Components.Dispose();<br /> }<br />// Release ONLY unmanaged resources. Ex: CloseHandle(handle);<br /> }<br /> disposed = true;<br /> }<br />protected volatile bool disposed = false;<br /> ~MyClass() <br /> {<br /> Dispose(false);<br /> }<br />}<br />[ComVisible(true)]<br />public interface IDisposable<br />{<br /> void Dispose();<br />}<br />
  32. 32. Using<br />using System;<br />using System.Collections.Generic;<br />using System.Diagnostics;<br />
  33. 33. Using<br />using System;<br />using System.Collections.Generic;<br />using System.Diagnostics;<br />using (MyClass c = new MyClass())<br />{<br />//Do Some Work<br />}<br />
  34. 34. Demo 5 - optimize<br />using System;<br />using System.Collections.Generic;<br />using System.Diagnostics;<br />using System.IO;<br />public class MyClass<br />{<br /> public static void RunSnippet()<br /> {<br /> while(true)<br /> {<br /> Stopwatch watch = new Stopwatch();<br />watch.Start();<br /> using(StreamReadersr = new StreamReader(@"C:…Garbage Collectioncatcher.txt"))<br /> {<br /> string line = "";<br />intwordCount = 0;<br /> while((line = sr.ReadLine()) != null)<br /> {<br />wordCount += line.Split().Length;<br /> }<br />Console.WriteLine("{0} Words", wordCount);<br /> }<br />watch.Stop();<br />Console.WriteLine(watch.ElapsedMilliseconds + " Milliseconds");<br /> }<br /> }<br />
  35. 35.
  36. 36. TotalRelocatedFinal Gen 0 Gen 1 Large Object Heap6,578,038 96,608 5,057,272 1,400,580 12 3,535,4645,473,972 101,501 1,441,201 2,097,172 103,992 9,328======== ======== ======== ========= ======== =========== -1,104,066 +4,893 -3,617,071 +696,592 +103,980 -3,526,136<br />
  37. 37. Design patterns and Memory<br />
  38. 38. Design patterns and Memory<br />Circular References.<br />Form<br />Control<br />Control<br />Control<br />Control<br />+Controls<br />+Parent<br />+Parent<br />+Parent<br />+Parent<br />
  39. 39. Design patterns and Memory<br />public class Preferences <br />{<br />public Preferences instance;<br />public static Preferences GetPrefs()<br /> {<br />if (instance == null)<br />instance = new Preferences();<br /> return instance;<br /> }<br />public event PrefsChanged;<br />}<br />
  40. 40. Design patterns and Memory<br />Rooted objects (Singletons).<br />Form<br />Control<br />Control<br />Control<br />Control<br />Preferences<br />+Controls<br />+Parent<br />+Parent<br />+Parent<br />+Parent<br />$GetPrefs<br />+PrefsChanged<br />
  41. 41. Design patterns and Memory<br />Lists, Hashtables, Dictionaries, etc.<br />Control<br />Control<br />Control<br />T<br />List<T><br />+Parent<br />+Parent<br />+Parent<br />…<br />
  42. 42. Design patterns and Memory<br />public class Foo<br />{<br />public static void DoSomething()<br /> {<br /> List<Bar> bars;<br />...<br />//Do Something<br />bar.Clear();<br /> }<br />}<br />
  43. 43. Design patterns and Memory<br />public class Foo<br />{<br /> static Dictionary<string, Bar> _bars;<br />public static Foo()<br /> {<br />//Initialize the Lookup table<br /> _bars = new Dictionary<string, Bar>();<br /> _bars.Add(“EndUp”, new Bar());<br />...<br /> }<br />}<br />
  44. 44. Take Aways<br />Don’t keep objects around unless you know you will be using them again.<br />Save these techniques for objects that are expensive to create and are frequently used.<br />Carefully consider use of type initializers and statics. <br />Consider caching patterns so memory can be reclaimed if needed.<br />If you are using observable patterns be sure you unsubscribe properly<br />
  45. 45. Contact & Reference Material<br />Ronn Black rblack@btsoft.org<br />http://msdn.microsoft.com/en-us/library/ms973837.aspx (Garbage Collector Basics and Performance Hints)<br />http://www.microsoft.com/downloads/details.aspx?FamilyID=a362781c-3870-43be-8926-862b40aa0cd0&DisplayLang=en (CLR Profiler for .Net 2.0)<br />http://www.openasthra.com/multithreading/heap-overview/ (Heap Overview)<br />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<br />http://msdn.microsoft.com/en-us/magazine/cc534993.aspx (Large Object Heap Uncovered)<br />http://msdn.microsoft.com/en-us/library/aa970850.aspx (Weak Event Patterns)<br />

×