

Visualizing Garbage Collection in Ruby and Python - danso
http://patshaughnessy.net/2013/10/24/visualizing-garbage-collection-in-ruby-and-python/

======
JulianMorrison
I wonder how this compares to Go?

~~~
a-priori
I'm not really familiar with Go, but according to their documentation, the Go
runtime currently uses a mark-and-sweep collector. So, at the level of detail
this article is talking about, it's similar to the Ruby one. The details may
be different though.

------
polskibus
Mark and sweep is also used in .NET and JVM, although I keep on forgetting
what was the main advantage over ref. counting.

~~~
TheLoneWolfling
Reference counting doesn't work well with circular references or multiple
threads. It also requires an extra field for every object.

There are ways around parts of this, but most people just moved to mark and
sweep instead.

(One can use thread-local counters and a global map of what threads have at
least one reference to an object, updated only when a reference goes from 0 to
1 or vice versa. Weighted reference counting is an alternative. There are also
a couple ways of detecting circular references.)

~~~
lojack
The author's second article also contains information on how Python deals with
circular references (a tracking garbage collector very similar to mark-and-
sweep).

[http://patshaughnessy.net/2013/10/30/generational-gc-in-
pyth...](http://patshaughnessy.net/2013/10/30/generational-gc-in-python-and-
ruby)

It's also worth noting that you don't always need to deal with circular
references in the case of short running programs. Additionally you can
eliminate the possibility of ever creating a circular reference by using an
immutable language.

~~~
thinkpad20
Speaking of immutable languages, there are actually tricks in Haskell by which
it can actually generate circular references because of its laziness (ML, on
the other hand, is immutable and strict, so it cannot, by default anyway).

In regards to reference counting vs. garbage collection, one advantage to
reference counting is that it can be more memory efficient, since objects do
not unnecessarily accumulate in memory. It can be advantageous in embedded
systems for this reason. GC can provide a speed boost at a memory usage cost,
because `malloc` and `free` are relatively expensive, and using a garbage
collector can reduce the number of these calls needed.

