7 ms·
There is a nasty source of "false retention" in conservative, stack-walking GC: pointers themselves! It's not just integer variables (and other data) which "loo
by kazinator 12y ago
There is a nasty source of "false retention" in conservative, stack-walking GC: pointers themselves! It's not just integer variables (and other data) which "look" like pointers.
Here is the scenario: a local variable in the C program holds the only pointer to a heap object. The object is no longer needed, so the program overwrites it with NULL. Now it is garbage, right? Wrong! The C compiler is optimizing, and has no clue about garbage collection (unlike, say, a proper Lisp compiler). The C compiler performs a data flow analysis and comes to conclusion that "ptr = NULL" is a dead assignment because the variable has no "next use" in the program graph. The assignment is optimized away, and so a memory location, or a register, continue pointing to the object. Sometimes this happens in some top-level function, and so during some long-lived execution of lower level functions, memory is occupied. Only if you're lucky, the memory location or register get reused for something else and so the stale pointer is overwritten.
If you're using infinite lazy lists, and are counting on the prefix of these lists to be garbage-collected, as you extend the tail, spurious retention of the list head is the kiss of death to your program.