Sunday, November 26, 2017

Garbage Collector Undestanding

In this article I have shared some points related to .NET Garbage Collector as follows.. 1. Destructor 2. Idisposable interface and Pattern 3. Using block 4. Dispose vs Finalize 5. GC (Garbage Collector) Some key points to remember about GC. 1. GC is a.net framework thread, which run in background. 2. GC check for all the objects which has destructor, if found then it will place them in another list named as Finalization List. 3. GC create two thread, The first thread:
  1. Which are reachable list (All reachable object are clared one after one from the heap list and this it reclaim the memory)
  2. Another which are not (which are not reachable is known as finalization list)
In 2nd thread,
  1. which reads the finalization lists and calls, the each object finalized in separate object.
How does GC Runs when:
  • Gen 0 Object reach ~256KB keep
  • Gen 1 objects reach ~2Meg
  • Gen 2 objects reach ~10meg
  • System memory is low.
What is managed and unmanaged code?
  1. .NET c#,vb Classes are known as Managed Object. All those which has been created using .net language, gc treated as a managed code.
  2. (vb6, com ,windows, connection, files) all these type of code or we can say non .net developed code are known as unmanaged code.
What is finalize method? Note: If your classes is using unmanaged code then start implement finalise method. If your classes is not using unmanaged code then forget about Finalize method call, don't do this at all because it's not required. How garbage collector call the Finalize method. As we all know that we have 3 gen where memory got collected. GC frequently check the Generation-0 bucket.
  1. Checking frequency in the Finalize queue is very less as compare to gen (0,1,2,)      bucket.
  2. By defining the destructor you can define the finalize method.
  3. When Code is complied the distructor will convert into the finalize    automatiocally.  gc treat this as a finalize, Moreover, in .net reflector you can see that after compilation the destructor convert into finalize method.
  4. We can not call directly finalize method because developer don't have any access to finalize .
  5. Only GC has the rights and facility to call the finalize method.
Advantages of using the GC. (Suppose a application uses a costly external resources, then a way to explicitly release the resource before the garbage collector frees the object.  a ->  Define a method to release allocated resources. (mscorlib.dll) Characteristics of Destructor
  1. Destructors (~) cannot be defined in Structs.
  2. Destructors (~) are only used with classes.
  3. Destructor cannot be inherited or overloaded.
  4. Destructor does not take modifiers or have parameters.
  5. Destructor cannot be called. They are invoked automatically.
  6. An instance becomes eligible for destruction when it is no longer possible for any code to use the instance.
  7. The Programmer has no control over when destructor is called because this is determined by Garbage Collector.
  8. Destructor is called when program exits.
  9. Execution of the destructor for the instance may occur at any time after the instance becomes eligible for destruction.
  10. Destructor implicitly calls Finalize on the base class of object.
  I have taken some references from c-sharpcorner.com, stackoverflow, codeproject, pluralsight and through some youtube videos.

No comments:

Popular Posts