Reputation: 455
Can any one please explain me.
What is the algorithm of garbage collector?
How garbage collector works in background?
Upvotes: 1
Views: 1602
Reputation: 107940
GCs only occur when the heap is full. When the garbage collector starts running, it makes the assumption that all objects in the heap are garbage. In other words, it assumes that none of the application's roots refer to any objects in the heap. Now, the garbage collector starts walking the roots and building a graph of all objects reachable from the roots. For example, the garbage collector may locate a global variable that points to an object in the heap.
Following Figure shows a heap with several allocated objects where the application roots 1 refer directly to objects Obj1, Obj2 and application root 2 refer to Obj4 and obj5. All of these objects become part of the graph. When adding object Obj2 of application root 1, the collector notices that this object refers to object Obj7 is also added to the graph. The collector continues to walk through all reachable objects recursively.
...Continued here
Here are a couple of other links to get you reading on Garbage collection:
Upvotes: 2
Reputation:
Here is the basic idea for the GC.
==========================================
Every application has a set of roots. Roots identify storage locations, which refer to objects on the managed heap or to objects that are set to null.
For example:
-- All the global and static object pointers in an application. -- Any local variable/parameter object pointers on a thread's stack. -- Any CPU registers containing pointers to objects in the managed heap. -- Pointers to the objects from Freachable queue
The list of active roots is maintained by the just-in-time (JIT) compiler and common language runtime, and is made accessible to the garbage collector's algorithm
Upvotes: 1