4 ms·
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)
by bioweek 17y ago
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)
- coliveira 17y agoA 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.
- bioweek 17y agowhy do you mean by accessible? Wouldn't that still mean reference counting, i.e., it's not accessible if it has 0 references?
- jeffcoat 17y agoHere'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.
- j_baker 17y agoI 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_science)#Reachability_of_an_object http://en.wikipedia.org/wiki/Garbage_collection_(computer_sc...