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.

Garbage collection in .net (basic level)

701 views

Published on

Garbage collection in .net (basic level)

Published in: Technology
  • Be the first to comment

Garbage collection in .net (basic level)

  1. 1. GARBAGE COLLECTION IN .NET (BASIC LEVEL) Larry Nung
  2. 2. AGENDA 2
  3. 3. AGENDA Function Feature Managed Heap Small Object Heap (SOH) Large Object Heap (LOH) Collection With Finalization Queue Collection Timing Strong reference & Week reference GC Mode Reference Q & A 3
  4. 4. FUNCTION 4
  5. 5. FUNCTION  Automatic memory management  Responsible for reclaiming the memory of no longer used objects. 5
  6. 6. FEATURE 6
  7. 7. FEATURE  Allow us to develop an application without having worry to free memory.  Allocates memory for objects efficiently on the managed heap.  Reclaims the memory for no longer used objects and keeps the free memory for future allocations.  Provides memory safety by making sure that an object cannot use the content of another object. 7
  8. 8. MANAGED HEAP 8
  9. 9. MANAGED HEAP  A series of allocated memory segments (approx 16Mb in size each) to store and manage objects 9
  10. 10. SMALL OBJECT HEAP (SOH) 10
  11. 11. GENERATIONS  Generation 0  The youngest generation and contains short-lived objects.  About 256 KB  Generation 1  Contains short-lived objects and serves as a buffer between short-lived objects and long-lived objects.  About 2MB  Generation 2.  Contains long-lived objects.  About 10MB 11
  12. 12. PROMOTIONS  Objects that are not reclaimed in a garbage collection are known as survivors, and are promoted to the next generation. 12
  13. 13. LARGE OBJECT HEAP (LOH) 13
  14. 14. LARGE OBJECT HEAP (LOH)  Feature  Never compacts 14
  15. 15. LARGE OBJECT HEAP (LOH) 15
  16. 16. COLLECTION WITH FINALIZATION QUEUE 16
  17. 17. COLLECTION WITH FINALIZATION QUEUE 17
  18. 18. COLLECTION WITH FINALIZATION QUEUE 18
  19. 19. COLLECTION WITH FINALIZATION QUEUE 19
  20. 20. COLLECTION WITH FINALIZATION QUEUE 20
  21. 21. COLLECTION TIMING 21
  22. 22. COLLECTION TIMING  Low physical memory.  The memory allocated on the managed heap surpasses an acceptable threshold.  Invoke System.GC.Collect. 22
  23. 23. STRONG REFERENCE & WEEK REFERENCE 23
  24. 24. STRONG REFERENCE & WEEK REFERENCE  Week reference  A reference that does not protect the referenced object from collection by a garbage collector. 24
  25. 25. STRONG REFERENCE & WEEK REFERENCE using System; public class Program { public static void Main() { var person1 = new Person("Larry"); var person2 = new WeakReference(null); person2.Target = new Person("Jack"); GC.Collect(); var jack = person2.Target as Person; Console.WriteLine(person1.Name); Console.WriteLine(jack == null ? "(null)" : jack.Name); } } public class Person { public string Name { get; private set; } public Person(string name) { this.Name = name; } } 25 person1 person2 Larry Heap Jack Strong Reference Week Reference person1 Larry Heap Strong Reference GC
  26. 26. GC MODE 26
  27. 27. GC MODE  Workstation GC  Default GC mode.  Server GC  Available only on multiprocessor computers.  Creates a separate managed heap and thread for each processor and performs collections in parallel.  ASP.NET and SQL Server enable. 27
  28. 28. REFERENCE 28
  29. 29. REFERENCE  Garbage Collection  https://msdn.microsoft.com/en- us/library/0xy59wtx(v=vs.110).aspx  .NET 的自動記憶體管理  https://msdn.microsoft.com/zh- tw/library/dd229208.aspx?f=255&MSPPError=- 2147217396  .Net Garbage Collection in depth  http://www.dotnet- tricks.com/Tutorial/netframework/0L3P131012-.Net- Garbage-Collection-in-depth.html 29
  30. 30. REFERENCE  Understanding .NET Garbage Collection  http://www.telerik.com/blogs/understanding-net- garbage-collection 30
  31. 31. Q&A 31
  32. 32. QUESTION & ANSWER 32

×