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

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: