Garbage Collection in .NET

383
-1

Published on

Memory management algorithms overview, explanation how garbage collector works in .NET? comparing to other systems.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
383
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
36
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Garbage Collection in .NET

  1. 1. • • • • • •
  2. 2. • • • • •
  3. 3. • • • Roots
  4. 4. • • • Roots
  5. 5. • • • Roots
  6. 6. + + + - -
  7. 7. • • root 1 2 0 1 2 0 1 0 2 3 1
  8. 8. • • root 1 2 0 1 2 0 1 0 2 3 1
  9. 9. • • root 1 1 2 1 1 0 2 1
  10. 10. • • root 1 1 2 1 1 1
  11. 11. • • root 1 1 2 1 1 1 1 1 1 1
  12. 12. + + + - -
  13. 13. • • • • • • old space root a b new space c d
  14. 14. • • • • • • old space root a b new space c d
  15. 15. • • • • • • old space b new space d root a c
  16. 16. • • • • • • new space old space root a c
  17. 17. + + + + -
  18. 18. Tracing Collection style Reference counting Copying collections batch incremental copy long short long Real Time no yes no Delayed Reclamation yes no no none high low yes no yes Pause Times Cost per mutation Collects cycles
  19. 19. Roots Processes global stack stack stack CPU registers
  20. 20. Roots Processes global stack stack stack CPU registers
  21. 21. Roots Processes global stack stack stack CPU registers
  22. 22. • public class Fin { public FileStream fs; Fin() { fs = new FileStream("text.txt", FileMode. Create); } ~Fin() { fs.Close(); } }
  23. 23. • • • • • • • •
  24. 24. a b d g c e h Finalization queue f i F-reachable queue
  25. 25. c d e a b d g h c e h Finalization queue f i F-reachable queue
  26. 26. c d e a b d g h c e h Finalization queue f i F-reachable queue
  27. 27. d a b d h c e c f e h F-reachable queue Finalization queue
  28. 28. d a b d h f e e h F-reachable queue Finalization queue
  29. 29. d a b h f d Finalization queue h F-reachable queue
  30. 30. • • ~Fin() { someGlobalVar = this; }
  31. 31. • • • • • • •
  32. 32. a b c d e d e 0 a b c 0 a b 1 d 0
  33. 33. a b d f g h b f d d 2 g k i h 0 1 a j j k 0 1 a i g i 1 0
  34. 34. • • • •
  35. 35. • • • public void WriteToFile(string s) { TextWriter tw = new StreamWriter("text.txt", true); tw.Write("new text"); TextWriter tw2 = new StreamWriter("text.txt", true); //??? }
  36. 36. • // For not-sealed classes protected virtual void Dispose(bool disposing) { } // For sealed classes private void Dispose(bool disposing) { } • • •
  37. 37. • • • public void Dispose() { Dispose(true); GC.SuppressFinalize(this); }
  38. 38. void Dispose(bool disposing) { if (disposing) { // Managed resources } // Unmanaged resources } • ~Fin() { Dispose(false); }
  39. 39. • using(TextWriter tw = new StreamWriter("text.txt", true)) { tw.Write("new text"); }
  40. 40. • • • • • •
  41. 41. • • • •
  42. 42. • • • • •
  43. 43. • • • "Incremental garbage collection fixes the problem by dividing the work of a GC into smaller pieces. Rather than do a 500 millisecond garbage collection, an incremental collector might divide the work into fifty slices, each taking 10ms to complete. In between the slices, Firefox is free to respond to mouse clicks and draw animations.“ http://blog.mozilla.org/javascript/2012/08/28/incremental-gc-in-firefox-16/
  44. 44. • • • •
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×