Index

GARBAGE-COLLECTOR tagged Questions - Page 1

What is dispose and finalize method and how it works


  1. Finalize and Dispose methods comes under the System.GC namespace and used to free up the memory occupied the objects. Garbage collector uses these objects to free up the memory.
  2. When there is no enough memory to allocate the new object in the Heap, Garbage collector gets called automatically to free up the memory. It mainly creates the object graph and finds the objects which are not used since longer time. Based on the generations, it removes the objects.
  3. Dispose method is called manually by programmer to remove the objects.
  4. It uses IDisposable interface which has the Dispose method and this can be implemented in the code.

What is Garbage Collection? How is Garbage Collector invoked in .NET?


When the heap memory is filled with the objects and there is no memory left to accommodate the new object, Garbage collector gets called by the CLR. So it's an activity which is initiated by the run time (CLR). When the garbage collector gets called, it tries to find out each referenced objects which are in use by the applications. The objects which are not reachable, it marks them to delete. Based on the reachable objects, the garbage collector prepares the object graph, which has the reference of each reachable object. Now based on the object graph which contain the object generations too, the Garbage collector checks for the generation 0 objects and marks them for deletion and move rest of the objects to generation 1. The heap memory gets compacted for the new object and new object gets placed in the heap memory.
This is the uncertain and un-deterministic process as the Garbage collector doesn't know when it will be called. Its all based on the capacity memory of the heap. When the heap memory get filled and a new object is initiated, the runtime (CLR) calls the Garbage collector.

What is Garbage Collection? How is Garbage Collector invoked in .NET?


When the heap memory is filled with the objects and there is no memory left to accommodate the new object, Garbage collector gets called by the CLR. So it's an activity which is initiated by the run time (CLR). When the garbage collector gets called, it tries to find out each referenced objects which are in use by the applications. The objects which are not reachable, it marks them to delete.

Based on the reachable objects, the garbage collector prepares the object graph, which has the reference of each reachable object. Now based on the object graph which contain the object generations too, the Garbage collector checks for the generation 0 objects and marks them for deletion and move rest of the objects to generation 1. The heap memory gets compacted for the new object and new object gets placed in the heap memory.

This is the uncertain and un-deterministic process as the Garbage collector doesn't know when it will be called. Its all based on the capacity memory of the heap. When the heap memory get filled and a new object is initiated, the runtime (CLR) calls the Garbage collector.

Interesting facts about garbage collector - .Net


Garbage collector is the heart of .NET Framework. The purpose of the Garbage Collector in .NET is to manage the allocation and release of memory in your application. 

 

1. Don’t create big objects with short lifetime

Any object in CLR that is more than 85 000 KB in size are stored in the special heap for big objects. Also they are marked as 2nd generation objects from the start. Creating big objects with small lifetime is bad practice. Why? Garbage collector will need to collect 2nd generation objects more often, and this could lead to performance issues.

 

2. When GC collects garbage it pauses all managed threads

When garbage collection starts CLR hijacks all managed threads and pauses them. But in fact it doesn’t affect coding, so… nevermind :)

 

3. You can predict OutOfMemoryException

In some cases if application consume a lot of resources there is a probability of OutOfMemoryException. Not good, yeah? But… you can predict for operations with big memory consumption by using MemoryFailPoint.

Usage example:

try

{

   using(MemoryFailPoint mfp = new MemoryFailPoint(1500))

   {

    //Run code which uses lots of memory.

   }

}

catch(InsufficientMemoryExpection)

{

    Console.WriteLine("Not enough memory.");

}

But be aware. Even if MemoryFailPoint didn’t thrown exception and memory is reserved it’s physically still not allocated. So you only increased a probability of successful memory allocation.

4. Garbage collection takes less than 1ms in zero generation

Yes, it’s so fast.

5. Developers could create immortal objects

Of course it’s only just for fun and definitely bad practice, but these objects will never die (read as not collected by GC).

6. You can manage unmanaged resources.

Sometimes a small managed object could contain a lot of unmanaged resources. For these cases there are 2 methods in GC class.

- AddMemoryPressure

- RemoveMemoryPressure

Using them you can hint how much unmanaged memory is used by object and this will affect when garbage collection will be started.

Similar Tags