Jaco de Groot wrote:
> > >
> > > Too many open files in system
> >
> > Make sure you're closing the files when you're done with them.
>
> I've checked my code for this. But even without closing them
> myself, shouldn't they be closed at garbage collecting time?
>
> What tool under Linux do I use to check how many files are
> currently open in my system?
As long as the File objects (or whatever Stream you're using on them) is
open, it is *absolutely guaranteed* not to be garbage collected. As far
as the VM knows, you're still using the reference, so it won't free the
memory being used by it. If you're opening a file to read from, then be
sure to close it when the read is completed (and if you want it to be
GCed, assign the reference to "null"). If you're opening a file to write
to it, close it when you're done writing.
On a read, you can use a while loop to check for end of file:
//open a file - we'll call it "f"
// get a stream on it - let's call it "s"
while (s.read != -1) { // -1 signals EOF
// do something useful
}
f.close();
Of course, you have to catch the possible IOException, so wrap that in a
try-catch block.
On a write, you can call flush() on the stream, then close the stream.
I don't know what tools there may be for finding out how many files are
open at a given time on Linux.
I hope this helps. :)
--
Jeff Galyan
http://www.anamorphic.com
http://www.sun.com
jeffrey dot galyan at sun dot com
talisman at anamorphic dot com
Sun Certified Java(TM) Programmer
======================================================================
Linus Torvalds on Microsoft and software development:
"... if it's a hobby for me and a job for you, why are you doing such a
shoddy job of it?"
The views expressed herein do not necessarily reflect those of my
employer.
Sun Microsystems, Inc., has no connection to my involvement with the
Mozilla Organization.
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]