.NET Fundamentals Garbage Collection Woody Pewitt [email_address]
 
 
Why Multi Generational?
Concurrent/Synchronous aka workstation and server <ul><li>Concurrent garbage collection is used in desktop applications </...
 
The good <ul><li>Cost of doing an allocation is extremely low </li></ul><ul><li>You don’t manage memory or pointers! </li>...
Issues to watch for <ul><li>Too Many Allocations </li></ul><ul><ul><li>String.Split </li></ul></ul><ul><ul><li>String conc...
 
What's wrong with this? string x = &quot;&quot;; for (int y = 0; y < 100; y++) { x += this.randomString(); } Strings are  ...
Use StringBuilder! string x = &quot;&quot;; StringBuilder y = new StringBuilder(); for (int i = 0; i < 100; i++) { y.Appen...
Finalization <ul><li>Objects that need finalization live longer than objects that do not </li></ul><ul><li>Objects that ne...
IDisposable and Dispose <ul><li>This interface provides an alternative method for reclaiming resources whose lifetime is w...
Weak References <ul><li>http://msdn.microsoft.com/en-us/library/system.weakreference.aspx </li></ul>
Induced Collections <ul><li>GC.Collect  </li></ul><ul><ul><li>Just don’t do it! </li></ul></ul><ul><li>Use  GCCollectionMo...
Consider <ul><li>Allocate all of the memory to be used with a given data structure at the same time </li></ul><ul><li>Remo...
Upcoming SlideShare
Loading in …5
×

.Net Garbage Collector 101

1,665 views

Published on

Given to the San Diego .NET Developers group for the fundamentals talk at the December meeting.

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

  • Be the first to like this

No Downloads
Views
Total views
1,665
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
49
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

.Net Garbage Collector 101

  1. 1. .NET Fundamentals Garbage Collection Woody Pewitt [email_address]
  2. 4. Why Multi Generational?
  3. 5. Concurrent/Synchronous aka workstation and server <ul><li>Concurrent garbage collection is used in desktop applications </li></ul><ul><li>Synchronous is used in server applications such as ASP.NET by default. </li></ul><ul><li>Concurrent mode, .NET will try to avoid stopping the running program while a collection is in progress </li></ul><ul><li>In synchronous mode, .NET will suspend the running application while the garbage collector is running </li></ul>
  4. 7. The good <ul><li>Cost of doing an allocation is extremely low </li></ul><ul><li>You don’t manage memory or pointers! </li></ul>
  5. 8. Issues to watch for <ul><li>Too Many Allocations </li></ul><ul><ul><li>String.Split </li></ul></ul><ul><ul><li>String concatenation </li></ul></ul><ul><li>Too-Large Allocations </li></ul><ul><li>Too Many Pointers </li></ul><ul><ul><li>Transitory objects with a lot of interdependencies </li></ul></ul><ul><li>Too Many Roots </li></ul><ul><li>Too Many Almost-Long-Life Objects </li></ul>
  6. 10. What's wrong with this? string x = &quot;&quot;; for (int y = 0; y < 100; y++) { x += this.randomString(); } Strings are immutable objects in the CLR!
  7. 11. Use StringBuilder! string x = &quot;&quot;; StringBuilder y = new StringBuilder(); for (int i = 0; i < 100; i++) { y.Append(this.randomString()); } x = y.ToString();
  8. 12. Finalization <ul><li>Objects that need finalization live longer than objects that do not </li></ul><ul><li>Objects that need finalization cause collateral damage </li></ul><ul><li>Objects needing finalization create work for the finalizer thread </li></ul>
  9. 13. IDisposable and Dispose <ul><li>This interface provides an alternative method for reclaiming resources whose lifetime is well known to the programmer </li></ul>class X: Idisposable { public X() { //initialize resources} ~X() { //release resources } public void Dispose() { // this is the same as calling ~X() Finalize(); // no need to finalize later System.GC.SuppressFinalize(this); } }
  10. 14. Weak References <ul><li>http://msdn.microsoft.com/en-us/library/system.weakreference.aspx </li></ul>
  11. 15. Induced Collections <ul><li>GC.Collect </li></ul><ul><ul><li>Just don’t do it! </li></ul></ul><ul><li>Use GCCollectionMode if you must! </li></ul><ul><ul><li>Default </li></ul></ul><ul><ul><li>Forced </li></ul></ul><ul><ul><li>Optimized </li></ul></ul>
  12. 16. Consider <ul><li>Allocate all of the memory to be used with a given data structure at the same time </li></ul><ul><li>Remove temporary allocations that can be avoided </li></ul><ul><li>Minimize the number of times object pointers get written </li></ul><ul><li>Reduce the density of pointers in your data structures. </li></ul><ul><li>Make limited use of finalizers! </li></ul>

×