closeOnExit

R

Roedy Green

Normally I would use Runtime.getRuntime().addShutdownHook( new
Thread() to ensure a file were closed automatically at shutdown.

I could not find any docs one way or the other, but, under any
circumstances does this happen automatically anyway?

I seem to vaguely recall reading the answer was no, but it was changed
in a recent JDK. Enlightenment?
--
Roedy Green Canadian Mind Products
http://mindprod.com

"There is an evil which ought to be guarded against, in the indefinite accumulation of property,
from the capacity of holding it in perpetuity by... corporations.
The power of all corporations aught to be limited in this respect.
The growing wealth acquired by them never fails to be a source of abuses."
~ James Madison (born: 1751-03-16 died: 1836-06-28 at age: 85)
 
M

Mike Schilling

Roedy said:
Normally I would use Runtime.getRuntime().addShutdownHook( new
Thread() to ensure a file were closed automatically at shutdown.

I could not find any docs one way or the other, but, under any
circumstances does this happen automatically anyway?

I seem to vaguely recall reading the answer was no, but it was
changed
in a recent JDK. Enlightenment?

Closed, or flushed? In all the OSs that are likely to be relevant
(Windows, Unix, Linux, VMS, etc.) a file is closed when the process
that has opened it exits. There's no need for the JVM to do anything
special.
 
R

Roedy Green

Closed, or flushed? In all the OSs that are likely to be relevant
(Windows, Unix, Linux, VMS, etc.) a file is closed when the process
that has opened it exits. There's no need for the JVM to do anything
special.

Flushed/closed. The OS is supposed to close the files in the sense of
freeing locks even if the JVM crashes.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"There is an evil which ought to be guarded against, in the indefinite accumulation of property,
from the capacity of holding it in perpetuity by... corporations.
The power of all corporations aught to be limited in this respect.
The growing wealth acquired by them never fails to be a source of abuses."
~ James Madison (born: 1751-03-16 died: 1836-06-28 at age: 85)
 
N

Nigel Wade

Christian said:
Though one should be aware that not alle ressources are cleaned up
immnediately...

Especially on Linux my esperince till now was that if Sockets were not
closed properly under some circumstances they stayed open for some more
time/on restarting the app ports were still in use...

Christian

That should only happen if one end does not close the connection cleanly. It's
part of the requirement for reliable/guaranteed delivery in TCP/IP, so all OS
which implement TCP/IP properly will show this behaviour.

The reason is that, even though your end of the socket may have been closed
cleanly, until the other end receives all remaining in-transit data and an
acknowledgement of that receipt has been received by your end the socket will
not be fully closed down. The socket enters a close-wait state, which can last
for some time. During this period the local port will not be available for
re-use, and re-transmission will be occurring.

You can enable the port for re-use if you wish (using the SO_REUSEADDR socket
option). However there is a caveat (isn't there always?). If the remote end
previously sent data which your end failed to read (for whatever reason) this
data will have remained in transit (which is probably why the port is not
available). When you re-open the port that remaining data will be delivered,
and that is the first data you will receive. If you are not ready to accept and
deal with that data (and it can be any amount of unknown data) it's best not to
enable that option.
 
M

Mike Schilling

Roedy said:
Flushed/closed. The OS is supposed to close the files in the sense
of
freeing locks even if the JVM crashes.

It will, but the OS can't flush Java's internal buffers.
 
R

Roedy Green

I seem to vaguely recall reading the answer was no, but it was changed
in a recent JDK. Enlightenment?

I was thinking of something like FORTRAN where the run time flushes
and closes all open files on exit, in other words close is optional,
more of an optimisation to close early.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"There is an evil which ought to be guarded against, in the indefinite accumulation of property,
from the capacity of holding it in perpetuity by... corporations.
The power of all corporations aught to be limited in this respect.
The growing wealth acquired by them never fails to be a source of abuses."
~ James Madison (born: 1751-03-16 died: 1836-06-28 at age: 85)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top