Mark and Sweep algorithm for GC

http://www.cnblogs.com/tekkaman/archive/2012/07/05/2578131.html
http://stackoverflow.com/questions/24799297/circular-reference-memory-leak/24963376#24963376

This algorithm reduces the definition of “an object is not needed anymore” to “an object is unreachable”.

  This algorithm assumes the knowledge of a set of objects called roots Periodically, the garbage-collector will start from these roots, find all objects that are referenced from these roots, then all objects referenced from these, etc. Starting from the roots, the garbage collector will thus find all reachable objects and collect all non-reachable objects.

  This algorithm is better than the previous one since “an object has zero reference” leads to this object being unreachable. The opposite is not true as we have seen with cycles.

  As of 2012, all modern browsers ship a mark-and-sweep garbage-collector. All improvements made in the field of JavaScript garbage collection (generational/incremental/concurrent/parallel garbage collection) over the last few years are implementation improvements of this algorithm, but not improvements over the garbage collection algorithm itself nor its reduction of the definition of when “an object is not needed anymore”.

Cycles are not a problem anymore

In the first above example, after the function call returns, the 2 objects are not referenced anymore by something reachable from the global object. Consequently, they will be found unreachable by the garbage collector.

The same thing goes with the second example. Once the div and its handler are made unreachable from the roots, they can both be garbage-collected despite referencing each other.

Limitation: objects need to be made explicitly unreachable

Although this is marked as a limitation, it is one that is rarely reached in practice which is why no one usually cares that much about garbage collection.

Javascript GC

Currently, Javascript V8’s garbage collection algorithm adopts the mark and sweep algorithm.
Hence, you do not have to worry about circular references.

Modern JavaScript implementations perform garbage collection through a “mark and sweep” algorithm. First they scan through your web app’s entire memory structure starting from the global object, and mark everything they find. Then they sweep through every object stored in memory and garbage collect anything that wasn’t marked. As long as there isn’t a reference to your object from the global object or any stored function, it can be garbage collected.

In Apple’s xCode, the obj-c garbage collector does not. Hence you DO NEED to worry and eliminate any code that has a circular reference, which includes parent/child, blocks, and delegates.