4 ms·
I want a language where the garbage collector not only frees inaccessible dynamically allocated blocks, but also frees all resources when they're inaccessible.
by ErotemeObelus 7y ago
I want a language where the garbage collector not only frees inaccessible dynamically allocated blocks, but also frees all resources when they're inaccessible. Is it possible to design such a generalized GC?
- jarjarbinks455 7y agoIn c# you can implement IDisposable. The garbage collector will call a dispose method allowing you piggyback on the garbage collector for just about anything. But it's rarely a good idea to piggyback. Most resources are much more limited or time sensitive than memory. So you should free them immediately. Even when your resource -is- memory it sometimes is a very very very bad idea to free via garbage collection.
- ErotemeObelus 7y agoWhat is even the point of garbage collection? By the time you manage all the resources of a 100k line application you're going to have to do as much work as if you wrote it in C.
- jarjarbinks455 7y agoWell the point of gc is to allow the algorithm to focus on the problem domain. and not worry about mechanical implementation details of a computer. Everything has a trade off. An obvious win still must pay a cost elsewhere. In the case of GC it's simply not a good trade off for many resources. Sometimes it cannot be used even if you were willing to accept a trade.
- ErotemeObelus 7y agoManaging resources by hand is managing the mechanical implementation details of a computer. So in order to have garbage collection that manages more than just a sliver of memory so small that it counts as a rounding error by the same you get to the size of real world projects, you need a custom operating system and possibly even custom hardware too.
- AnimalMuppet 7y agoYour statement is false for many real world projects. (For some, you are correct. Those projects should not be written in a garbage-collected language, or should be written to run on a custom OS and custom hardware.)
- erik_seaberg 7y agoWhen GC'd objects represent resources like open connections and files, use-after-free safely and reliably raises an exception to handle. Without GC, use-after-free on pretty much any object is so catastrophic that even correct code starts doing unpredictable things.
- Someone 7y agoGC can do that, for a loose definition of when they’re inaccessible (it will do it when, but not as soon as) Reference counting almost gives you as soon as, but it has trouble handling circular references. That is fixable, but not, AFAIK, in a way that’s both reasonably performance, frees resources as soon as they become unreachable, and isn’t too restricting.
- bjourne 7y agoWhat fix are you referring to? The ones I know of are algorithms for detecting when the object graph becomes disconnected. Unfortunately that turns out to be very hard in practice and you often have to scan a large portion of the heap. So it doesn't offer any major advantage over reference counting + mark & sweep gc. Which is kind of the worst of both worlds. :/ Ref counting overhead + potentially long gc pause times.
- Someone 7y agoOnes that are either not reasonably performant, don’t free resources as soon as they become unreachable, or are too restricting. The fix you refer to is one, but it falls under ‘not reasonably performant’. One could also make it illegal to have cycles (easy way to do that: make it illegal to update any references). For many, many, that will fall under ‘too restricting’, though. Alternatively, allow updates to references, but give every allocated object a ‘bit’ that indicates whether, from it, a reference can be reached that contains such an updated reference that points to an object created later (maintaining that property is doable, but may not be ‘reasonably performant’) You can use that later: when the reference count of an object goes to zero, and the object has a reference to an object O with that bit set, check whether the graph reachable from O can be reached from elsewhere. Again, that’s doable, but likely not performant. It somewhat depends on how common circular references are in real world code, though. I’m not aware of any papers researching that.
- tjansen 7y agoJava's GC does it. When a reference becomes unreachable, it will automatically free the resource. That's not the issue. The issue is that GCs are not predictable. All current implementations either have issues with circular references, or they run infrequently and may even need several runs to clean up complex structures. So it may take a while until an unused resource becomes available again, and you don't know when.