5 ms·
Java does memory recovery strictly by garbage collection. To delete an object remove all references to that object and wait. A "delete now" operation imposes
by shemnon42 9y ago
Java does memory recovery strictly by garbage collection. To delete an object remove all references to that object and wait.
A "delete now" operation imposes requirements on implementations that are not strictly required.
Any advantages gained from destructors can be realized via explicit cleanup methods and (as a hedge against mis-use) state checking for initialized and destroyed objects. A delete operation also adds risk that an object that has been deleted may be referenced again (although with esoteric stuff like PhantomReferences you can almost do it).
- bobsgame 9y agoWould it be impossible (or prohibitively difficult) to add it to an implementation, though?
- shemnon42 9y agoProhibitively difficult. If you really truly need delete then you really truly don't need Java and should use something like C.
- bitwize 9y agoWhat about the "automatically gets cleaned up when the object goes out of scope" advantage of destructors?
- guipsp 9y agoYou can do that with try-with-resources
- foota 9y agoI could see where this might be useful for reducing gc load in an application with (very) soft real time requirements.