Memory Leak , Garbage Collector

  22th Day

Hey Guys, I hope that you are Enjoying with my blogs writing and in previous blog we have  learnt about Primitive Data Type, And  then see Stack, Heap, Value Type and Reference Type and Today's we will Memory Leak , Garbage Collector


Memory Leak
Memory Leak Occurs when a programmer create a memory in heap and forget to delete it. To avoid this leak memory allocated on heap should always be freed when no longer needed.

All Primitive Data type runs on stack , after end of the scope it release the memory space automatically at a time because its memory location is Contiguous while in Heap doesn't.

Garbage Collector
When class object is created at runtime, certain memory space allocated to it in the Heap memory .After all action of objects are completed in the program there has no longer need of memory allocated space in that situation Garbage Collector automatically release the memory space. 


  • Garbage Collector runs in the background its a background process.
  • It runs un-deterministically it means we doesn't know when it will come to release space.in that case application stops for naino sec.
          Lets see the code garbage collector is run or not.

for(int i=0i<100000i++)
  {
    //line1
     int i = 10;
     int y = i;            
  }

Line1: In that only has primitive Data type, so garbage Collector will not come to release the memory. Because it release automatically. 
let's see Click on debug -> Performance Profiler -> Memory Usage and Start 


 So we can see in the above snapshot there are no appear GC in primitive Data type. 


C# Code , To see the Garbage Collector

for(int i=0i<100000i++)
  {
       //line1
        int i1 = 10;
        int y = 10;
      // line2
        Customer x = new Customer();
        x.name = DateTime.Now.ToString();
        Thread.Sleep(50);
 }

              Line2: In that Case we can see the garbage collector in red-red sign randomly.




                            You must watch this video to more details learning.



                                                 Thank You...!



No comments:

Post a Comment