5 ms·
Closing files manually is manual resource management, not memory management. C++ uses its memory management for resource management, but Java doesn't. Java imp
by tjansen 7y ago
Closing files manually is manual resource management, not memory management.
C++ uses its memory management for resource management, but Java doesn't. Java improved resource management with "try with resources", but generally resource management really isn't an issue for most Java developers. In modern frameworks like Spring you rarely have to manage resources yourself. You let the framework manage them for you, and the framework will provide them to you via injection.
- AnimalMuppet 7y agoYour way of saying it isn't wrong, but I think I would say instead that C++ uses destructors both for memory management and management of other resources.
- brianush1 7y agoIf we're being pedantic here, destructors don't manage memory at all, they only free resources. Memory is freed by the caller of the destructor, either automatically if the memory was allocated on the stack, or manually by the delete operator if the memory was allocated on the heap.
- Someone 7y agoMemory is a resource, too, and C++-style destructors do manage it, sometimes. Destructors do not free the memory of the object being destroyed, but they can free memory that its fields point to.
- ErotemeObelus 7y agoI 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.)