JEP 132: More-prompt finalization
Alan Bateman
Alan.Bateman at oracle.com
Fri Jan 6 06:58:27 PST 2012
On 06/01/2012 10:26, Tony Printezis wrote:
>
> I agree. And I'm sure I can dig up some of my slides from past talks
> that make this exact point: if you know you're done with an object,
> please call close() on it. However, as it's already been pointed out
> in an earlier reply to this thread, it's not always possible to rely
> on close(): reclaiming DirectByteBuffers that are not guaranteed to be
> unreachable could be a security issue.
Just to mention that in Java SE then all APIs for sockets and files
implement Closeable so they have a close method, can be used with the
try-with-resources construct where appropriate. While some of the older
APIs (FileInputStream, FileOutputStream, the default SocketImpls) have
finalizers, the newer APIs do not and so the resources must be
explicitly closed.
As you mention, direct and mapped buffers can't be explicitly reclaimed.
We don't have a good solution to that problem which is why System.gc is
invoked when limits are reached. It would be nice to re-visit this some
day. Off-hand I think this is the only other place in the JDK, aside
from RMI DGC, where System.gc is invoked explicitly.
-Alan.
More information about the hotspot-dev
mailing list