THE CHRONICLES OF GARBAGE COLLECTION
BY
Rupreet Singh Gujral (rupreetg@outlook.com)
Product Development / Architect / Entr...
AGENDA
• Memory Concepts
• Basic Principles
• Advantages and Disadvantages of using GC
• GC Algorithm
• Setting the contex...
MEMORY CONCEPTS
• Physical Memory: This is the physical RAM on the machine/device. This is mainly used by kernel
drivers.
...
ARE YOU GUYS AWAKE?
• Suppose user is running an application on a x64 bit machine.
• The machine has 4GB RAM installed.
• ...
OK, LET ME HELP YOU!
• Application always allocate memory in its “address space”. If the
allocated “address space” is exha...
BASIC PRINCIPLES!
• Find objects that cannot be accessed in the future
• Reclaim/Release the resources used by those objec...
ADVANTAGES AND DISADVANTAGES
+ Automatic memory reclaim of objects that are not required
+ Memory Leaks are taken care of ...
LETS SEE HOW MANY GARBAGE OBJECTS
WE CAN FIND?
GC ALGORITHM
• Reference Counting: Each object has a flag telling if it is referenced or not.
• Mark and Sweep: Two round ...
SETTING THE CONTEXT
• Reachability: GC depends on reachability heavily – to see if objects are reachable
or not.
• Roots: ...
CLR IMPLEMENTATION OF GC
Object A
Object B
Object C
Managed Heap
NextObjPtr
Allocations
CLR IMPLEMENTATION OF GC
Image Source: MSDN
Allocated Objects in Heap
Managed Heap after Collection
Collection Algorithm
CLR IMPLEMENTATION OF GC
Image Source: MSDN
Finalization
Heap with multiple objects Managed Heap after Garbage Collection
CLR IMPLEMENTATION OF GC
Finalization Cont…
Managed Heap after Second Garbage Collection
# Objects with Finalize needs two...
BEST PRACTICES RELATED TO MEMORY
MANAGEMENT IN .NET
• Assigning NULL to an object when its not needed doesn’t collect the ...
Q&A
Aim and shoot your questions!
Image source: http://egamer.co.za/2011/07/review-shadows-of-the-damned/
THANK YOU!
Image Source: http://www.comicvine.com/forums/battles-7/spiderman-vs-wesker-670859/
Upcoming SlideShare
Loading in …5
×

Chronicles Of Garbage Collection (GC)

465 views

Published on

This indepth session talks about the basic concept of Memory Management and then Garbage Collector. It discusses in detail about .NET implementation of GC and best practice from personal experience

Published in: Technology
2 Comments
0 Likes
Statistics
Notes
  • Thanks! Let me know what exactly you are looking for and we can probably have something up in our next session in Techizzaa! (https://www.facebook.com/Techizzaa)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Very nice presentation Rupreet. I was looking for more on managed vs unmanaged code too.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Be the first to like this

No Downloads
Views
Total views
465
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
11
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide

Chronicles Of Garbage Collection (GC)

  1. 1. THE CHRONICLES OF GARBAGE COLLECTION BY Rupreet Singh Gujral (rupreetg@outlook.com) Product Development / Architect / Entrepreneur
  2. 2. AGENDA • Memory Concepts • Basic Principles • Advantages and Disadvantages of using GC • GC Algorithm • Setting the context • CLR implementation of GC • Best practices related to memory management in .NET
  3. 3. MEMORY CONCEPTS • Physical Memory: This is the physical RAM on the machine/device. This is mainly used by kernel drivers. • Virtual Memory: It’s the memory emulation Operating System does for user to store and access data. Its not related to physical memory. Virtual Memory is used at system level. • Address space: This is specific to the memory allocated/used by a process. Threads in a process use address space for their stack, etc. Address space is used at process level. • Private bytes: Private Bytes is the current size, in bytes, of memory that this process has allocated that cannot be shared with other processes. • Working Set: The Working Set is the set of memory pages touched recently by the threads in the process.
  4. 4. ARE YOU GUYS AWAKE? • Suppose user is running an application on a x64 bit machine. • The machine has 4GB RAM installed. • Task Manager shows 2GB of total RAM is used and rest is free • After running application, it throws “Out Of Memory” (OOM) exception. How?
  5. 5. OK, LET ME HELP YOU! • Application always allocate memory in its “address space”. If the allocated “address space” is exhausted, only then you’ll get OOM exception even if Virtual Memory is available
  6. 6. BASIC PRINCIPLES! • Find objects that cannot be accessed in the future • Reclaim/Release the resources used by those objects HISTORY Garbage collection was invented by John McCarthy around 1959 to solve problems in Lisp
  7. 7. ADVANTAGES AND DISADVANTAGES + Automatic memory reclaim of objects that are not required + Memory Leaks are taken care of (almost!) + Bugs like dangling pointers, double free, etc + Free developer from manually taking care of memory mgmt. - Performance Impact; consume resources - Pauses in application - Non deterministic, not real time
  8. 8. LETS SEE HOW MANY GARBAGE OBJECTS WE CAN FIND?
  9. 9. GC ALGORITHM • Reference Counting: Each object has a flag telling if it is referenced or not. • Mark and Sweep: Two round collection cycle. First round it marks objects which are live and in second round it clears the unused objects • Copying Collectors: Create two heaps of same size. Use one for allocations; when filled, pause the system and copy objects to other. • Mark Compact Collector: Extended version of Mark and Sweep. In the second phase, it compacts the heap to avoid fragmentation.
  10. 10. SETTING THE CONTEXT • Reachability: GC depends on reachability heavily – to see if objects are reachable or not. • Roots: Starting point for GC to traverse the memory. Global & Static variables, etc • Generations: Memory divided into generation. GC collects objects specific to generation, avoiding full memory scan. • Managed Heap: Process “continuous” address space which is governed by CLR/GC • Determinism: GC are non-deterministic in the time of object finalization.
  11. 11. CLR IMPLEMENTATION OF GC Object A Object B Object C Managed Heap NextObjPtr Allocations
  12. 12. CLR IMPLEMENTATION OF GC Image Source: MSDN Allocated Objects in Heap Managed Heap after Collection Collection Algorithm
  13. 13. CLR IMPLEMENTATION OF GC Image Source: MSDN Finalization Heap with multiple objects Managed Heap after Garbage Collection
  14. 14. CLR IMPLEMENTATION OF GC Finalization Cont… Managed Heap after Second Garbage Collection # Objects with Finalize needs two GCs for cleanup. Avoid having Finalize methods # Finalize are different from destructor # Any exception in Finalize should be handled and return as if nothing happened. This helps other Finalize method to execute # Developers doesn’t control the execution (and the order) of Finalize methods # Finalizable objects should avoid referring to non- Finalizable objects. Break the object into two types and let the light weight have Finalize
  15. 15. BEST PRACTICES RELATED TO MEMORY MANAGEMENT IN .NET • Assigning NULL to an object when its not needed doesn’t collect the object instantly. • While designing classes, if it has some unmanaged resources, do implement Dispose Pattern and Finalize method • In Dispose implementation, do call GC.SuppressFinalize method. • If the class has Close/Dispose method, it’s the responsibility of the caller to call them. Finalize methods are implemented here in case caller forgets to call them • Avoid calling GC.Collect(..);
  16. 16. Q&A Aim and shoot your questions! Image source: http://egamer.co.za/2011/07/review-shadows-of-the-damned/
  17. 17. THANK YOU! Image Source: http://www.comicvine.com/forums/battles-7/spiderman-vs-wesker-670859/

×