5 ms·
In c# you can implement IDisposable. The garbage collector will call a dispose method allowing you piggyback on the garbage collector for just about anything.
by jarjarbinks455 7y ago
In 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.