SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Memory management is the main concern for any application whether application is window based or
   web based. In .Net, CLR has garbage collector that executes as a part of our program and responsible for
   reclaiming the memory of no longer used objects. Garbage collector free the memory for objects that are
   no longer referenced and keeps the memory for future allocations.

   Advantage of Garbage Collector
1. Allow us to develop an application without having worry to free memory.
2. Allocates memory for objects efficiently on the managed heap.
3. -Reclaims the memory for no longer used objects and keeps the free memory for future allocations.
4. Provides memory safety by making sure that an object cannot use the content of another object.

   Memory Allocation in Managed Heap
   The managed heap is a series of allocated memory segments (approx 16Mb in size each) to store and
   manage objects. The memory for newly created object is allocated at the next available location on the
   managed heap. If there is available free memory, the garbage collector doesn't search the dead objects
   for memory reclaim and memory allocations has been done very fast. If the memory is insufficient to
   create the object, the garbage collector search the dead objects for memory reclaim for the newly object.
An object is created using the new operator. This operator first makes sure that the bytes required by the
   new     object   fit   in   the   reserved   region   (committing   storage   if   necessary).   If   the   object
   fits, NextObjPtr points to the object in the heap and object's constructor is called and the new operator
   returns the address of the object.

   Key points about Garbage Collector
1. All objects in the heap are allocated from one contiguous range of memory address and heap is divided
    into generations so that it is easy to eliminate the garbage objects by looking at only a small fraction of
    the heap.
2. Gen 0 and Gen 1 occupy a single segment known as the ephemeral segment. Gen 2 is a set of further
    segments and the large object heap is yet another group of segments.
3. Almost, all objects with-in a generation are of the same age.
4. The newest objects are created at higher memory address while oldest memory objects are at lowest
    memory address with in the heap.
5. The allocation pointer for the new objects marks the boundary between the allocated and free memory.
6. Periodically the heap is compacted by removing the dead objects and sliding up the live objects towards
    the lower memory address end of the heap as shown in above fig.
7. The order of objects (after memory reclaims) in memory remains the same as they were created.
8. There are never any gaps among the objects in the heap.
9. Only some of the free memory is committed when required and more memory is acquired from the OS in
    the reserved address range.

   Generations in Managed Heap
   The managed heap is organized into three generations so that it can handle short lived and long lived
   objects efficiently. Garbage collector first reclaim the short lived objects that occupy a small part of the
   heap.
1.      Generation 0
This is the youngest generation and contains the newly created objects. Generation 0 has short-lived
objects and collected frequently. The objects that survive the Generation 0 are promoted to Generation 1.
Example : A temporary object.
2.      Generation 1
This generation contains the longer lived objects that are promoted from generation 0. The objects that
survive the Generation 1 are promoted to Generationen 2. Basically this generation serves as a buffer
between short-lived objects and longest-lived objects.
3.      Generation 2
This generation contains the longest lived objects that are promoted from generation 1 and collected
infrequently.
Example : An object at application level that contains static data which is available for the duration of the
process.

Garbage Collector Working Phase
1.      Marking Phase
In this phase garbage collector finds and creates a list of all live objects.
2.      Relocating Phase
In this phase garbage collector updates the references to the objects that will be compacted.
   3.      Compacting Phase
   In this phase garbage collector reclaims the memory occupied by the dead objects and compacts the
   surviving objects. The compacting phase moves the surviving objects toward the older end of the
   memory segment.




   Note
1. The large object heap is not compacted, because copying large objects imposes a performance penalty.

   Garbage Collection Algorithm
   Garbage collector determine whether any object in the heap is dead or not being used by the application.
   If such objects exist then memory used by these objects can be reclaimed. But how garbage collector
   know about these objects?
   Each and every application has a set of roots and these identify the storage locations for the objects on
   the managed heap.
Example : All the global, static objects pointers and all the local variable/ parameter object pointers on
the thread's stack in the application are considered part of the appilcation's roots. More over any CPU
registers containing pointers to objects in the managed heap are also considered a part of the
application's roots.
The list of active roots is maintained by the JIT compiler and CLR, and is made accessible to the garbage
collector's algorithm.
Memory Reclaim Process
Now the garbage collecor starts go through the roots and make a graph of all the objects reachable from
the roots. The below fig. shows a heap with allocated objects. In this heap the application roots directly
refer to the objects 1,3,4,6 and object 3 & 6 refers to the objects 8 & 10. Hence all these objects will
bacome the part of the live objects graph.




The objects which are not reachable from application's roots, are considered as garbage since these are
not accessible by the application. In above heap objects 2,5,7,9 will be considered as dead objects.
The garbage collector then remove the dead objects from the heap and live objects will move toward the
older end of the memory segment as shown in below fig. Garbage collector also updates all the
references(including root references) to the moving objects in the heap.




What do you think?
I hope you will enjoy these deep tips about garbage collector in .net framework. I would like to have
feedback from my blog readers. Your valuable feedback, question, or comments about this article are
always welcome.

Weitere ähnliche Inhalte

Andere mochten auch

From Java Code to Java Heap: Understanding the Memory Usage of Your App - Ch...
From Java Code to Java Heap: Understanding the Memory Usage of Your App  - Ch...From Java Code to Java Heap: Understanding the Memory Usage of Your App  - Ch...
From Java Code to Java Heap: Understanding the Memory Usage of Your App - Ch...jaxLondonConference
 
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Java Garbage Collectors – Moving to Java7 Garbage First (G1) CollectorJava Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Java Garbage Collectors – Moving to Java7 Garbage First (G1) CollectorGurpreet Sachdeva
 
Performance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersPerformance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersNLJUG
 
Java memory presentation
Java memory presentationJava memory presentation
Java memory presentationYury Bubnov
 
From Java code to Java heap: Understanding and optimizing your application's ...
From Java code to Java heap: Understanding and optimizing your application's ...From Java code to Java heap: Understanding and optimizing your application's ...
From Java code to Java heap: Understanding and optimizing your application's ...Chris Bailey
 
Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Ashish Jha
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage CollectionAzul Systems Inc.
 
Getting ready to java 8
Getting ready to java 8Getting ready to java 8
Getting ready to java 8DataArt
 
Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8AppDynamics
 
50 new things you can do with java 8
50 new things you can do with java 850 new things you can do with java 8
50 new things you can do with java 8José Paumard
 
3장. Garbage Collection
3장. Garbage Collection3장. Garbage Collection
3장. Garbage Collection김 한도
 
55 New Features in Java SE 8
55 New Features in Java SE 855 New Features in Java SE 8
55 New Features in Java SE 8Simon Ritter
 
What's New in Java 8
What's New in Java 8What's New in Java 8
What's New in Java 8javafxpert
 
Java 8 Support at the JVM Level
Java 8 Support at the JVM LevelJava 8 Support at the JVM Level
Java 8 Support at the JVM LevelNikita Lipsky
 

Andere mochten auch (15)

From Java Code to Java Heap: Understanding the Memory Usage of Your App - Ch...
From Java Code to Java Heap: Understanding the Memory Usage of Your App  - Ch...From Java Code to Java Heap: Understanding the Memory Usage of Your App  - Ch...
From Java Code to Java Heap: Understanding the Memory Usage of Your App - Ch...
 
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Java Garbage Collectors – Moving to Java7 Garbage First (G1) CollectorJava Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
Java Garbage Collectors – Moving to Java7 Garbage First (G1) Collector
 
Performance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen BorgersPerformance van Java 8 en verder - Jeroen Borgers
Performance van Java 8 en verder - Jeroen Borgers
 
Java memory presentation
Java memory presentationJava memory presentation
Java memory presentation
 
From Java code to Java heap: Understanding and optimizing your application's ...
From Java code to Java heap: Understanding and optimizing your application's ...From Java code to Java heap: Understanding and optimizing your application's ...
From Java code to Java heap: Understanding and optimizing your application's ...
 
Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)Mark and sweep algorithm(garbage collector)
Mark and sweep algorithm(garbage collector)
 
Understanding Java Garbage Collection
Understanding Java Garbage CollectionUnderstanding Java Garbage Collection
Understanding Java Garbage Collection
 
Getting ready to java 8
Getting ready to java 8Getting ready to java 8
Getting ready to java 8
 
Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8Memory Management: What You Need to Know When Moving to Java 8
Memory Management: What You Need to Know When Moving to Java 8
 
50 new things you can do with java 8
50 new things you can do with java 850 new things you can do with java 8
50 new things you can do with java 8
 
3장. Garbage Collection
3장. Garbage Collection3장. Garbage Collection
3장. Garbage Collection
 
55 New Features in Java SE 8
55 New Features in Java SE 855 New Features in Java SE 8
55 New Features in Java SE 8
 
What's New in Java 8
What's New in Java 8What's New in Java 8
What's New in Java 8
 
Java SE 8 best practices
Java SE 8 best practicesJava SE 8 best practices
Java SE 8 best practices
 
Java 8 Support at the JVM Level
Java 8 Support at the JVM LevelJava 8 Support at the JVM Level
Java 8 Support at the JVM Level
 

Ähnlich wie Garbage collector complete information

Learn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionLearn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionDiya Singh
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4aminmesbahi
 
A novel design of a parallel machine learnt
A novel design of a parallel machine learntA novel design of a parallel machine learnt
A novel design of a parallel machine learntcseij
 
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector cseij
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Prashanth Kumar
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it worksMindfire Solutions
 
01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptxssuser95922e
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad ideaPVS-Studio
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMjaganmohanreddyk
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorWednesday Solutions
 
Garbage collection
Garbage collectionGarbage collection
Garbage collectionSomya Bagai
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android ApplicationsLokesh Ponnada
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory ManagementRahul Jamwal
 

Ähnlich wie Garbage collector complete information (20)

Learn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage CollectionLearn Microsoft Asp.Net Garbage Collection
Learn Microsoft Asp.Net Garbage Collection
 
.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4.NET Core, ASP.NET Core Course, Session 4
.NET Core, ASP.NET Core Course, Session 4
 
A novel design of a parallel machine learnt
A novel design of a parallel machine learntA novel design of a parallel machine learnt
A novel design of a parallel machine learnt
 
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
A Novel Design of a Parallel Machine Learnt Generational Garbage Collector
 
Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)Memory Management in the Java Virtual Machine(Garbage collection)
Memory Management in the Java Virtual Machine(Garbage collection)
 
Memory management
Memory managementMemory management
Memory management
 
Java Garbage Collection - How it works
Java Garbage Collection - How it worksJava Garbage Collection - How it works
Java Garbage Collection - How it works
 
01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx01class_object_references & 02Generation_GC.pptx
01class_object_references & 02Generation_GC.pptx
 
Unit 5
Unit 5Unit 5
Unit 5
 
Chapter 7 Run Time Environment
Chapter 7   Run Time EnvironmentChapter 7   Run Time Environment
Chapter 7 Run Time Environment
 
Why using finalizers is a bad idea
Why using finalizers is a bad ideaWhy using finalizers is a bad idea
Why using finalizers is a bad idea
 
Garbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVMGarbage Collection in Hotspot JVM
Garbage Collection in Hotspot JVM
 
iOS Memory Management
iOS Memory ManagementiOS Memory Management
iOS Memory Management
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
 
Memory management
Memory managementMemory management
Memory management
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Memory Leaks in Android Applications
Memory Leaks in Android ApplicationsMemory Leaks in Android Applications
Memory Leaks in Android Applications
 
C# basics
C# basicsC# basics
C# basics
 
memory
memorymemory
memory
 
C++ Memory Management
C++ Memory ManagementC++ Memory Management
C++ Memory Management
 

Kürzlich hochgeladen

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 

Kürzlich hochgeladen (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 

Garbage collector complete information

  • 1. Memory management is the main concern for any application whether application is window based or web based. In .Net, CLR has garbage collector that executes as a part of our program and responsible for reclaiming the memory of no longer used objects. Garbage collector free the memory for objects that are no longer referenced and keeps the memory for future allocations. Advantage of Garbage Collector 1. Allow us to develop an application without having worry to free memory. 2. Allocates memory for objects efficiently on the managed heap. 3. -Reclaims the memory for no longer used objects and keeps the free memory for future allocations. 4. Provides memory safety by making sure that an object cannot use the content of another object. Memory Allocation in Managed Heap The managed heap is a series of allocated memory segments (approx 16Mb in size each) to store and manage objects. The memory for newly created object is allocated at the next available location on the managed heap. If there is available free memory, the garbage collector doesn't search the dead objects for memory reclaim and memory allocations has been done very fast. If the memory is insufficient to create the object, the garbage collector search the dead objects for memory reclaim for the newly object.
  • 2. An object is created using the new operator. This operator first makes sure that the bytes required by the new object fit in the reserved region (committing storage if necessary). If the object fits, NextObjPtr points to the object in the heap and object's constructor is called and the new operator returns the address of the object. Key points about Garbage Collector 1. All objects in the heap are allocated from one contiguous range of memory address and heap is divided into generations so that it is easy to eliminate the garbage objects by looking at only a small fraction of the heap. 2. Gen 0 and Gen 1 occupy a single segment known as the ephemeral segment. Gen 2 is a set of further segments and the large object heap is yet another group of segments. 3. Almost, all objects with-in a generation are of the same age. 4. The newest objects are created at higher memory address while oldest memory objects are at lowest memory address with in the heap. 5. The allocation pointer for the new objects marks the boundary between the allocated and free memory. 6. Periodically the heap is compacted by removing the dead objects and sliding up the live objects towards the lower memory address end of the heap as shown in above fig. 7. The order of objects (after memory reclaims) in memory remains the same as they were created. 8. There are never any gaps among the objects in the heap. 9. Only some of the free memory is committed when required and more memory is acquired from the OS in the reserved address range. Generations in Managed Heap The managed heap is organized into three generations so that it can handle short lived and long lived objects efficiently. Garbage collector first reclaim the short lived objects that occupy a small part of the heap.
  • 3. 1. Generation 0 This is the youngest generation and contains the newly created objects. Generation 0 has short-lived objects and collected frequently. The objects that survive the Generation 0 are promoted to Generation 1. Example : A temporary object. 2. Generation 1 This generation contains the longer lived objects that are promoted from generation 0. The objects that survive the Generation 1 are promoted to Generationen 2. Basically this generation serves as a buffer between short-lived objects and longest-lived objects. 3. Generation 2 This generation contains the longest lived objects that are promoted from generation 1 and collected infrequently. Example : An object at application level that contains static data which is available for the duration of the process. Garbage Collector Working Phase 1. Marking Phase In this phase garbage collector finds and creates a list of all live objects. 2. Relocating Phase
  • 4. In this phase garbage collector updates the references to the objects that will be compacted. 3. Compacting Phase In this phase garbage collector reclaims the memory occupied by the dead objects and compacts the surviving objects. The compacting phase moves the surviving objects toward the older end of the memory segment. Note 1. The large object heap is not compacted, because copying large objects imposes a performance penalty. Garbage Collection Algorithm Garbage collector determine whether any object in the heap is dead or not being used by the application. If such objects exist then memory used by these objects can be reclaimed. But how garbage collector know about these objects? Each and every application has a set of roots and these identify the storage locations for the objects on the managed heap.
  • 5. Example : All the global, static objects pointers and all the local variable/ parameter object pointers on the thread's stack in the application are considered part of the appilcation's roots. More over any CPU registers containing pointers to objects in the managed heap are also considered a part of the application's roots. The list of active roots is maintained by the JIT compiler and CLR, and is made accessible to the garbage collector's algorithm. Memory Reclaim Process Now the garbage collecor starts go through the roots and make a graph of all the objects reachable from the roots. The below fig. shows a heap with allocated objects. In this heap the application roots directly refer to the objects 1,3,4,6 and object 3 & 6 refers to the objects 8 & 10. Hence all these objects will bacome the part of the live objects graph. The objects which are not reachable from application's roots, are considered as garbage since these are not accessible by the application. In above heap objects 2,5,7,9 will be considered as dead objects.
  • 6. The garbage collector then remove the dead objects from the heap and live objects will move toward the older end of the memory segment as shown in below fig. Garbage collector also updates all the references(including root references) to the moving objects in the heap. What do you think? I hope you will enjoy these deep tips about garbage collector in .net framework. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.