Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Python doesn't really have real GC, it refcounts. See the stackoverflow answer.


Python refcounts and it has a real mark+sweep collector for collecting cycles. It's not a dichotomy, you know.


The reference counting causes the problem here. When multithreading means that incrementing/decrementing a reference count is no longer deterministic, you need locks, or all hell breaks loose. Adding a mark&sweep GC isn't going to fix that.

While I'm not familiar with the specifics of Python's GC, a mark&sweep phase is usually added to reference counting so that if there's garbage which contains references to itself but has no external references, it will eventually be collected. (_Garbage Collection_ by Richard Jones and Rafael Lins is an excellent resource on GC details, btw. There's also a decent overview in the O'Reilly OCaml book (http://caml.inria.fr/pub/docs/oreilly-book/html/book-ora082.... )). In other words, it plugs the worst memory leaks caused by reference counting.

How to do multiprocessor / multithread GC well is still an area of active research. In the mean time, one simpler solution is to have several independent VM states, each running in their own thread (or process), and communicating via message passing. Lua makes this easy, but its VM is considerably lighter than Python's.


The mark and sweep collector is a little more complicated than that. It doesn't use a single "seen" bit the way a normal GC header would, instead it uses the refcount itself in very, very clever ways


How does a real GC work? I can't picture how you can do GC without ref counts.

(sorry if that's a dumb question)


I would disagree with saying that refcounting isn't "real" garbage collection.

But with that said, you can get all the gory details from wikipedia: http://en.wikipedia.org/wiki/Garbage_collection_(computer_sc...


A GC looks at the content of memory and checks if each piece of memory is accessible (there are sophisticated algorithms to do this) - no need for a ref count is necessary with GC.


why do you mean by accessible? Wouldn't that still mean reference counting, i.e., it's not accessible if it has 0 references?


Here's a (very naive) algorithm that doesn't count references:

1. Start at some well-known root object. Mark this object reachable.

2. For each object that is reachable

   2a. For each object that this reachable object has a reference to 

       2b. Mark this object reachable if it's not already.
3. If any object was marked reachable in 2b, repeat step 2.

4. Garbage collect every object not marked reachable.

To see an important difference between this kind of strategy ("mark and sweep") and reference counting, consider the classic example of two objects, each one of which has a link to the other, but are not visible to anything else in the system.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: