Hacker News new | ask | show | jobs
by shemnon42 3202 days 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).

3 comments

I could see where this might be useful for reducing gc load in an application with (very) soft real time requirements.
What about the "automatically gets cleaned up when the object goes out of scope" advantage of destructors?
You can do that with try-with-resources
Would it be impossible (or prohibitively difficult) to add it to an implementation, though?
Prohibitively difficult. If you really truly need delete then you really truly don't need Java and should use something like C.