|
|
|
|
|
by mayank
4733 days ago
|
|
> I'd think the "zeroth-order" assumption would be that you'd be keeping everything in that frame. Why would that be a reasonable assumption? If a variable cannot ever be referenced, it should be garbage collected. In the example, "str" cannot ever be referenced. This is why we use garbage collected languages. > but that's an optimization, not something you should be counting on. It's not an optimization, it's correct GC behavior, and it's absolutely something you should be counting on if you're using a GC language. |
|
I think I've used scheme implementations that have the same "feature", with less of an excuse.
It's a similar "failure of GC" if you create an object with two fields, like "obj={a: giant_value, b: 5}" then had your closure return obj.b. I bet the "a" slot would not be GC'd, even though there's no way to reach it.
If your mental model is that you're holding onto the lexical frame, it's a pretty much the same behavior.