Day 22 : Garbage Collector
Before going to garbage collector we are going to understand some other concepts :
Memory Leak :
- If a program does not return or release allocated memory space, even after the execution is done then it results slower or unresponsive system behavior which called as Memory Leak.
- A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.
- The .NET event listeners are the most common memory leak reason. We create a event and forget to remove it.
- We can avoid this by unsubscribe the subscribed event, use weak-handlers etc.
2. Static :
- Static objects live at GC root and they never collected by the collector.
- As we all know the static instances stay forever in memory which causes memory leak.
3. Cache :
- As all collections use cache, cache eventually runs out of memory.
We can check memory usage by following path :
Debug>Performance profiler >Memory usage >Start
- The stack memory is cleared automatically at the end of method.
- The CLR(Common Language Runtime) take charge to clear stack memory.
- The stack uses primitive data types i.e the primitive data type clears their memory as self responsibility they don't need GC.
- Example :
Garbage Collector :
- The garbage collector is a automatic memory manager which is responsible for allocation and release of memory.
- We can also say that "GC means regaining the memory assigned to objects which are not in use".
- GC always work on managed heap.
- Objects which are created and managed under CLR are called managed code.
- Object which are created and managed outside CLR (like database connections) are called unmanaged code.
- When there isn't enough memory to allocate object, GC must collect and release garbage memory to make memory available to new allocations.
- If the system has low physical memory, then garbage collection is necessary.
- As we now know that GC runs automatically, but in uncertain situation if we call GC.Collect() method then also GC occurs.
Phases in GC :
Their are 3 phases in GC :
1. Generation 0 :
- They are considered as a short-lived objects.
- The newly crated objects are placed here.
- These are considered as a buffer between short and long lived objects.
- They are considered as a long-lived objects.
- The static members and global variables are placed here.
Some points you may know :
- When GC starts scanning of objects it first scan Generation 0 and then determine which objects are again needed and shift them in Generation 1. If they needed again they shifted to Generation 2.
- When processing starts GC start creating a tree, and which objects are not present their they considered as garbage and get destroyed and GC gets memory back.
- When GC process comes to end, its GC responsibility to re-assign memory address of destroyed objects to living objects.
Advantages of GC :
- We don't need to free memory manually.
- It efficiently allocates objects on managed heap.
- It keeps memory available for future also.
Example :
Thank You!!!
For more understanding watch below video :
Comments
Post a Comment