Garbage can be created whenever a programmer reassigns a pointer. The first step to garbage detection is to determine the live objects. Remember that garbage collection is called during run-time whenever the program runs out of memory. We consider the variables in whatever function we are in at the time as well as the calling functions. For example, if both p and q point to x, then x has a reference count of 2.
Reference counting is interleaved with program execution.
Everytime we make a change involving pointers in the program, we must update their reference counts. Decrement the reference counts of whatever the object points to. So, we can reclaim obj1 and then decrement the reference count of whatever points to, which is obj-2. So, obj-3 now has a reference count of 0, and we can reclaim it. The biggest problem with the Reference Counting approach is cycles .