A Developer Journey who codes for fun

Daily Dose Of Code

  • Home
  • Dot.Net Basics
    • .Net Basics
      • CTS
      • CLS
      • CLR
      • Strong Vs Weak Ref
      • .Net Framework
      • What is Manifest
    • Memory Management
      • Garbage Collection 1
      • Garbage Collection 2
      • Circular Reference
  • C Sharp
    • Abstract Class in C#
    • Interfaces in C#
    • Value type by Val and By Ref
    • Var keyword
    • Null Coalescing Operator
    • Buit-in code snippets
  • OOPS
    • Abstraction and Encapsulation
    • Polymorphism
    • Inheritence
    • Aggregation
  • Threading
    • Delegates
      • Calling Delegate using Invoke, BeginInvoke
      • Multicast Delegate
      • Exception Handling in Multicast Delegate
      • Action
      • Predicate
      • Func
    • Synchronization
    • Thread Pool
    • Exception Handling
    • TPL
  • Design Pattern
    • Creational Patterns
      • Singleton Pattern
      • Factory Pattern
      • Abstract Factory Pattern
      • Prototype Pattern
      • Builder Pattern
    • Structural Patterns
      • Adapter Pattern
      • Bridge Pattern
      • Composite Pattern
      • Proxy Pattern
      • Facade Pattern
      • Decorator Pattern
      • Flyweight Pattern
    • Behavioral Patterns
      • Command Pattern
      • Interpreter Pattern
      • Iterator Pattern
      • Mediator Pattern
      • Memento Pattern
      • Observer Pattern
      • State Pattern
      • Strategy Pattern
      • Visitor Pattern
      • Chain Of Responsibility Pattern
      • Template Pattern
  • Data Structures
    • Generic List in C#
    • 2d array to 1d array
    • 3d arrayto 1d array
    • Linked List
      • Singly Linked List in C#
    • Queue
      • Dummy Data 1
    • Stack
      • Dummy Data 2
    • Tree
      • Dummy Data 3
    • Graph
      • Dummy Data 4
  • WCF
    • WCF Service using VS 2015
  • Scripts
    • Chrome Extensions
      • Create a Chrome Extension
      • Facebook autologout script
      • Gmail autologout script

Garbage Collection - Automatic memory management

 Unknown     4:44 AM     .Net Architechture     6 comments   

While thinking of this question few things are coming in my mind ~ How .Net reclaims objects and memory used by an application ? So the answer is Garbage Collector

Memory management is the main part of CLR and Garbage Collector does the job for CLR. Garbage Collector attempts to reclaim garbage memory or memory occupied by objects those are no longer use by the application.

Advantages of Garbage Collector
  1. Allocates memory efficiently in Managed Heap
  2. Reclaims objects that are no longer be used by the application and keeps the memory available for other objects.
  3. You do not need to worry about memory. You just write your code GC will take care of the things.
  4.  Provides safe memory so that an object can not use content of another one.
 By default on 32 bit system each process can have 2 GB of virtual address space and on 64 bit system process can have 8 TB of address space( There is lot more concepts behind to that in case if you are interested then visit here. As .net application developer you deal with virtual address space only, never communicate with the physical memory. Virtual address space is divided into some fragments called blocks. So when application request to allocate memory GC look after these blocks and tries to find single block that is large enough to fulfill your need. It will be unsuccessful to allocate memory if virtual memory allocation manager doesn't find single block.Each proccess has its own managed heap.

When Garbage Collection Occurs ?

  1. When system has low physical memory
  2. When a process is initialized run time reserves a contiguous region of address space that initially has no storage allocated for it. This region is known as managed heap. The heap maintains a pointer - NextObjPtr. This pointer indicates where the next object is to be allocated within the heap. Initially, this pointer is set to the base address of the managed heap.
When ever you creates an object in the application, operator makes sure that managed heap have enough space to store this object in reserved address space region. If that object get fits in the managed heap then NextObjPTR increments itself to point to new location. If managed heap doesn't have enough space to store the object or object can not be fit it the reserved space region then garbage collection must be performed. The heap detects this by adding the size of the new object to NextObjPtr. If pointer is beyond the end of the region, then the heap is full and a collection must be performed.
         
So now you know how Garbage Collection performed in an application but this was the over view. Garbage collector maintains some generations to manage objects. So now the question comes into the mind :

How Garbage Collector reclaims memory ?

The Garbage Collector checks whether the object in heap is still in use or it's dead or not being used by the application. If it's not being used by the application then Garbage Collector claims that memory. So now the question comes in the mind How GC knows whether this object is in use or not ?

So each application has some set of application roots and these roots are maintained by CLR. These are kind of pointers those points to the storage location and those location refer objects. If those objects are null then GC reclaims that.

Application Roots: Global objects, static pointers and local object pointers that resides under thread stack are considered as application roots. From these roots GC identifies the dead objects.


Now we can see that Application roots are pointing to four objects : Object c, object D, Object F and Object H but Object C is pointing towards Object G as well. So these objects are still referring in the application but other objects are dead or not referring in the application. GC marked those objects as Garbage. So this was the first phase of GC known as Marking Phase.

Second stage is Relocation and Compaction : In this GC traverse linearly through the Heap, looking for memory blocks of Garbage objects and consider it as free memory. Now shifts all the non garbaged objects down in the heap and update the references to the objects using memcpy method. Lets look over the new graph after this phase -

                                 
GC only occurs when managed heap is full. So that's how GC algorithm works. Now GC also have Generation concepts to improve performance.

Generation 0: It's a youngest generation and contains all newly created objects.

Generation 1: Objects, who survives after next GC occurs, then they promote to Generation 1.

Generation 2: It's for long lived objects like objects those exists through out the application like static objects.
  
I will talk more about Generations in my Next article.

Hope you enjoyed this article.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Clr - Common Language Runtime .Net framework provides a run time environment - CLR. Common language runtime takes the IL code from the compiler( language specific) and provide to … Read More
  • Garbage Collection - Automatic memory management While thinking of this question few things are coming in my mind ~ How .Net reclaims objects and memory used by an application ? So the answer is Gar… Read More
  • What is Common Language Specification ? CLS is more about rules of .Net and it's a subset of CTS(Common Type System). You must be thinking of CTS, don't worry will discuss in my next articl… Read More
  • Strong Vs Weak References Strong Reference -  As long as if application refers to the object. GC will not collect those objects. Those objects we say are strong reference… Read More
  • Garbage Collection - Automatic Memory Management Part II Welcome friends in the second article of Garbage Collection. Those who have missed the first one can visit here. So in this article i will talk more … Read More
Newer Post Older Post Home

6 comments:

  1. UnknownOctober 5, 2017 at 3:18 AM

    Thanks for sharing informative article. Download Windows 7 ultimate for free from getintopc. It helps you to explore full functionality of windows operating system.

    ReplyDelete
    Replies
      Reply
  2. Ben JohnsonMay 6, 2020 at 4:50 AM

    Thanks for sharing this blog..Appreciate your efforts..Keep writing..

    full stack developer course in chennai
    full stack developer course in chennai with placement
    full stack developer course with placement
    full stack developer course near me
    full stack developer course near me
    full stack developer course fees
    full stack developer course in chennai BITA Academy
    Uipath training in chennai
    Uipath training in chennai BITA Academy
    blue prism training in chennai
    blue prism training in BITA Academy
    rpa training in chennai
    rpa training in velachery
    rpa training in chennai velachery

    ReplyDelete
    Replies
      Reply
  3. rohanJune 17, 2020 at 12:19 AM

    Excellent Blog..has lots of information..Completely impressed..

    Placement Training in Chennai
    Placement Training in Chennai BITA Academy
    Linux Training in Chennai
    PERL Training in Chennai
    Security Testing Training in Chennai
    Security Testing Course in Chennai
    Selenium Training in Chennai
    Selenium Course in Chennai
    JMeter Training in Chennai
    Appium Training in Chennai

    ReplyDelete
    Replies
      Reply
  4. Scott StayrisOctober 21, 2020 at 10:58 PM

    Thank you for sharing this article. Quickbooks is one of the advanced accounting software to handle business related transactions. You can install quickbooks on mac also. For more information, visit our website.

    ReplyDelete
    Replies
      Reply
  5. AnonymousSeptember 5, 2023 at 12:49 AM

    ok

    ReplyDelete
    Replies
      Reply
  6. EnriqueSeptember 5, 2023 at 12:52 AM

    Garbage collection" refers to the automatic memory management process in computer programming. It involves the identification and removal of unused or "garbage" memory objects by the programming language's runtime environment. Minimum Spaces Needed This process helps prevent memory leaks and ensures efficient memory.

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

About The Author

Unknown
View my complete profile

Total Pageviews

84897

Popular Posts

  • Clr - Common Language Runtime
    .Net framework provides a run time environment - CLR. Common language runtime takes the IL code from the compiler( language specific) and p...
  • Auto logout chrome extension for Gmail
    Hello Friends, In the last article we learned to create a sample chrome extension. Here we are going to create auto logout Gmail script as...
  • Predicate delegate in C#
    Hello Everyone, In the article we will talk about Predicate delegate. Predicate is also a delegate which encapsulate a method that takes...
  • .Net Framework overview
    Hello friends : Here i am writing my first article on .Net framework anyways....So the question is What is .Net Framework ? The .Net fram...
  • Calling the Delegates using Invoke(), BeginInvoke() and DynamicInvoke() ?
    Hello Guys, So in the last article we talked about What is delegate and how can we create a delegate. In this article we will discuss w...
  • What does it mean by disconnected data access architecture of ADO.Net?
    ADO.Net introduces the concept of disconnected data architecture. In traditional data access components, you make a connection to the databa...
  • Nagarro Placement Papers..
    Ques.1 :- Seat Reservation prog for the theatre. Write a function for seat allocation for the movie tickets. Total no of seats available are...
  • C code to Check the string has valid identifier or not in.
    #include #include #include char keyword[][10]={"auto","break","case","char","const","...
  • Delegates in C Sharp
    A Delegate is a type variable that holds the reference to a method. Delegates are similar to Pointer to functions in C and C++ When we...
  • Garbage Collection - Automatic memory management
    While thinking of this question few things are coming in my mind ~ How .Net reclaims objects and memory used by an application ? So the ans...

Blog Archive

  • ►  2016 (4)
    • ►  September (2)
      • ►  Sep 03 (2)
    • ►  August (1)
      • ►  Aug 28 (1)
    • ►  April (1)
      • ►  Apr 24 (1)
  • ►  2015 (12)
    • ►  September (10)
      • ►  Sep 30 (1)
      • ►  Sep 29 (1)
      • ►  Sep 28 (1)
      • ►  Sep 27 (2)
      • ►  Sep 26 (3)
      • ►  Sep 20 (1)
      • ►  Sep 19 (1)
    • ►  August (1)
      • ►  Aug 16 (1)
    • ►  March (1)
      • ►  Mar 31 (1)
  • ▼  2013 (10)
    • ►  June (1)
      • ►  Jun 16 (1)
    • ►  April (1)
      • ►  Apr 21 (1)
    • ▼  February (8)
      • ►  Feb 18 (3)
      • ▼  Feb 17 (2)
        • Garbage Collection - Automatic Memory Management P...
        • Garbage Collection - Automatic memory management
      • ►  Feb 16 (2)
      • ►  Feb 15 (1)
  • ►  2012 (1)
    • ►  May (1)
      • ►  May 27 (1)
  • ►  2010 (22)
    • ►  October (14)
      • ►  Oct 21 (1)
      • ►  Oct 06 (12)
      • ►  Oct 04 (1)
    • ►  April (2)
      • ►  Apr 22 (1)
      • ►  Apr 16 (1)
    • ►  March (1)
      • ►  Mar 30 (1)
    • ►  January (5)
      • ►  Jan 08 (3)
      • ►  Jan 01 (2)
  • ►  2009 (110)
    • ►  December (8)
      • ►  Dec 18 (2)
      • ►  Dec 05 (1)
      • ►  Dec 04 (5)
    • ►  November (1)
      • ►  Nov 27 (1)
    • ►  October (14)
      • ►  Oct 09 (4)
      • ►  Oct 07 (1)
      • ►  Oct 06 (3)
      • ►  Oct 05 (3)
      • ►  Oct 01 (3)
    • ►  September (17)
      • ►  Sep 30 (1)
      • ►  Sep 29 (1)
      • ►  Sep 28 (1)
      • ►  Sep 25 (1)
      • ►  Sep 24 (1)
      • ►  Sep 17 (2)
      • ►  Sep 15 (3)
      • ►  Sep 11 (2)
      • ►  Sep 09 (3)
      • ►  Sep 08 (2)
    • ►  August (31)
      • ►  Aug 31 (1)
      • ►  Aug 27 (3)
      • ►  Aug 26 (1)
      • ►  Aug 25 (2)
      • ►  Aug 24 (1)
      • ►  Aug 22 (2)
      • ►  Aug 21 (3)
      • ►  Aug 20 (2)
      • ►  Aug 19 (3)
      • ►  Aug 18 (1)
      • ►  Aug 16 (1)
      • ►  Aug 12 (2)
      • ►  Aug 11 (1)
      • ►  Aug 10 (3)
      • ►  Aug 07 (4)
      • ►  Aug 06 (1)
    • ►  July (24)
      • ►  Jul 25 (4)
      • ►  Jul 24 (20)
    • ►  April (15)
      • ►  Apr 10 (3)
      • ►  Apr 07 (9)
      • ►  Apr 06 (3)

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments
copyright @ TechGiant 2015. Powered by Blogger.

Disclaimer

This is my personal blog and i write articles on .Net, WPF, C#, OOPS, Threading and other .Net technologies. This is not related to any of my employer and organizations. This is the result of my personal interest.

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Followers

Copyright © 2025 A Developer Journey who codes for fun | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com