Chronicles Of Garbage Collection (GC)

17
THE CHRONICLES OF GARBAGE COLLECTION BY Rupreet Singh Gujral ([email protected] ) Product Development / Architect / Entrepreneur

description

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

Transcript of Chronicles Of Garbage Collection (GC)

Page 1: Chronicles Of Garbage Collection (GC)

  THE CHRONICLES OF GARBAGE COLLECTION

BY

Rupreet Singh Gujral ([email protected])

Product Development / Architect / Entrepreneur

Page 2: Chronicles Of Garbage Collection (GC)

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

Page 3: Chronicles Of Garbage Collection (GC)

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.

Page 4: Chronicles Of Garbage Collection (GC)

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?

Page 5: Chronicles Of Garbage Collection (GC)

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

Page 6: Chronicles Of Garbage Collection (GC)

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

Page 7: Chronicles Of Garbage Collection (GC)

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

Page 8: Chronicles Of Garbage Collection (GC)

LETS SEE HOW MANY GARBAGE OBJECTS WE CAN FIND?

Page 9: Chronicles Of Garbage Collection (GC)

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.

Page 10: Chronicles Of Garbage Collection (GC)

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.

Page 11: Chronicles Of Garbage Collection (GC)

CLR IMPLEMENTATION OF GC

Object A

Object B

Object C

Managed Heap

NextObjPtr

Allocations

Page 12: Chronicles Of Garbage Collection (GC)

CLR IMPLEMENTATION OF GC

Image Source: MSDN

Allocated Objects in Heap

Managed Heap after Collection

Collection Algorithm

Page 13: Chronicles Of Garbage Collection (GC)

CLR IMPLEMENTATION OF GC

Image Source: MSDN

Finalization

Heap with multiple objects Managed Heap after Garbage Collection

Page 14: Chronicles Of Garbage Collection (GC)

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

Page 15: Chronicles Of Garbage Collection (GC)

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(..);

Page 16: Chronicles Of Garbage Collection (GC)

Q&A

Aim and shoot your questions!

Image source: http://egamer.co.za/2011/07/review-shadows-of-the-damned/

Page 17: Chronicles Of Garbage Collection (GC)

THANK YOU!

Image Source: http://www.comicvine.com/forums/battles-7/spiderman-vs-wesker-670859/