2 ms·
It's not. You're describing a static analyzer that inserts free() automatically. Not GC. GC is pretty much the opposite. Edit: Well, I didn't think it through.
by raincole 3y ago
It's not. You're describing a static analyzer that inserts free() automatically. Not GC. GC is pretty much the opposite.
Edit: Well, I didn't think it through. You're correct. A "perfect GC" is indeed undecidable, and all the real world, practical GC have "false positive" (a piece of memory is no longer used by the code, but there is no way to know it). A perfect GC without false positive is equal to halting problem.
Example:
var obj = new Object();
// === real code begin
... 50 lines of real code that never uses obj
if (something == true) return;
... another 50 lines of real code that never uses obj
// === real code end
obj.doThings();
A perfect GC can release obj while the real code is running. A real world, imperfect GC can't, since whether the real code returns early or not is undecidable.
- lisper 3y ago> You're correct. No, s/he's not. The original claim was: > It is in general undecidable whether an object is UNREACHABLE. [Emphasis added] Being unreachable is not the same thing as being "no longer used by the code". The latter is indeed undecidable, but the former is not.
- chubot 3y agoAfter writing a garbage collector, I can say that I’ve seen the unreachable / “will never be used” distinction, and it’s more or less irrelevant trivia. It doesn’t help you write a garbage collector to think of it that way. It doesn’t provide insight into the problem. The problems are elsewhere. Focus on reachability, dynamic understanding of the heap, performance, etc.
- nomel 3y ago> more or less irrelevant trivia What are some cases? The only I can think of is if a "remote" thing owned the memory. For example, if I sent a pointer elsewhere, then reused it later. Like, maybe with a device driver, or library.
- monocasa 3y agoYou normally fix that not with the GC itself, but with the compiler. If new Object() truly doesn't have any effects visible other than the valid reference, then it's creation can be moved to after the if statement.