4 ms·
I always think that GCs are incomplete in a way. They can reclaim memory in cases such as: x = [1, 2, 3] ... x = [] But not in cases such as:
by amelius 8y ago
I always think that GCs are incomplete in a way. They can reclaim memory in cases such as:
x = [1, 2, 3]
...
x = []
But not in cases such as:
x = [1, 2, 3]
... (x not used)
Of course, determining if x is used might be uncomputable in general, but in practice it might be computable in a lot of cases.
- kazinator 8y agoA compiler can determine that a local variable x has no "next use", and insert the x = [] at that point to help garbage collection (or else provide some map of what is live). https://en.wikipedia.org/wiki/Live_variable_analysis https://en.wikipedia.org/wiki/Live_variable_analysis
- umanwizard 8y agoI don't understand what you mean. Any GC will reclaim `x` after it is unreachable from a root.
- TheCoreh 8y agoUnreachable is not the same as unused It could still be reachable, just never read from. Static code analysis can detect some forms of this, but not all e.g. if (someUnknownCondition) { // access x }
- umanwizard 8y agoWell as the person I was replying to points out, this is uncomputeable in general (`someUnknownCondition` might be brute-forcing all possible mathematical proofs to see whether the Reimann hypothesis is true), and I would expect optimizing compilers to already handle this in the cases where it is feasibly computable. Is that not the case?
- arghwhat 8y agoIn this case, you cannot free the memory manually either without making your program potentially invalid. It is unreasonable to expect a GC to do runtime control flow analysis, which is also more than you would do in a normal case of manual memory management. If you have tricky situations where you know that program flow dictates that an expensive but reachable variable is no longer needed, you would insert a "x = nil" or similar in the position where you would otherwise have made a free. However, this is solely a case of fine-tuned optimization, rather than a case of correctness.
- zamalek 8y ago> without making your program potentially invalid You'd only need the GC to disconnect that local from the stack frame, as anything that escapes would be rooted elsewhere. This can be done by having the compiler/JIT emit a liveness map: instruction pointer ranges indicating which variables uninitialized, live or dead. Otherwise, exactly. Rust could do this with zero runtime overhead (as the map would be used by borrowck). This is pretty simple to do but generally isn't, precisely because of this: "this is solely a case of fine-tuned optimization." If you are part of the 0.1% of the people who is actually solving one the of 0.001% problems where this level of control is _genuinely_ important, then use a language that supports this. Assuming that the method is executing for a very long time (>μs), because this the only time you'd probably care about memory management at this level, any FFI is virtually free. However, even if you are part of the 0.1% of people who have a legitimate concern for this, and you are working on a 0.001% problem, why on earth are you doing so many allocations and frees? Against your own better judgement? If you care enough to worry about when memory is freed, you should know enough that you should re-use memory as much as possible without involving the allocator.
- zamalek 8y agoThe article I linked deals with this. GCs typically guarantee cleanup in a scope. With managed languages, this is whenever the runtime decides to run the GC (probably when memory is needed and isn't available). With Rust RAII this is when the ref count reaches zero which is, at the very soonest, at the end of the current scope (you can force the collection with a naked scope if you need[1]). [1]: https://play.rust-lang.org/?gist=66740557884b17683b868483e524c6ba&version=stable&mode=debug&edition=2015 https://play.rust-lang.org/?gist=66740557884b17683b868483e52...