4 ms·
> But the problem with most garbage collectors is that they only clean up memory! Is this an inherent limitation of garbage collectors, or could they be made t
by car_analogy 4y ago
> But the problem with most garbage collectors is that they only clean up memory!
Is this an inherent limitation of garbage collectors, or could they be made to clean up arbitrary resources?
- fancyfredbot 4y agoActually he later goes on to say the garbage collector can clean other resources by using the finalise method. The problem is that it doesn't necessarily clean them up at the right time. The garbage collector isn't triggered by having too many open file descriptors, it runs when you are low on memory. In theory it could look at other resources too, but for certain resources you don't want to wait at all.
- arcticbull 4y agoThere's also generally no guarantee that finalizers ever run, period.
- slaymaker1907 4y agoThat really depends a lot on the language. You could have a language which guarantees all finalizers are run so long as the process terminates normally (i.e. not by a power outage).
- Sesse__ 4y agoIt is obvious if you think of what a garbage collector is; it's a simulation of having infinite memory. So objects in a GC world live forever, and thus are never destroyed. (Of course, behind the scenes, the garbage collector will _deallocate_ them, but only when your program can no longer care because it doesn't have any way of reaching them.) Of course, a garbage collector can be made to run finalizers, but then you either have to do a pure refcounting GC, at least for those objects (which is highly suboptimal for speed), or live with unpredictable finalization (which is incompatible with using RAII for e.g. locks, as the article points out).
- pornel 4y agoSome people consider reference counting a form of garbage collection, and in such case you can have deterministic destruction of any kind of resource (PHP and Swift use that).