find max number of open file descriptors using java

P

puzzlecracker

I am writing a server which can have maximum number of simultaneous
connection equal to the maximum number of open file descriptors. How
can it be enforced in java as well as best design strategy?

Thanks
 
G

Gordon Beaton

I am writing a server which can have maximum number of simultaneous
connection equal to the maximum number of open file descriptors. How
can it be enforced in java as well as best design strategy?

The operating system will enforce the limit for you. Your application
will fail to open additional files, sockets etc after reaching the
descriptor limit.

/gordon
 
C

Chris Brat

Hi,

This just seems like a bad idea to me.

In practice if this single server actually holds your max number of
simultaneous connections then you wont be able to do anything else on
that OS. No other applications will be able to open any other
resources.

I'd rather impose a configurable maximum number of simultaneous
connections.

My 2c

Chris
 
G

Gordon Beaton

In practice if this single server actually holds your max number of
simultaneous connections then you wont be able to do anything else
on that OS. No other applications will be able to open any other
resources.

On most platforms I know, you'll be stopped by a per-process limit
long before you reach any system wide limit.

/gordon
 
C

Chris Uppal

Gordon said:
On most platforms I know, you'll be stopped by a per-process limit
long before you reach any system wide limit.

Not to mention that in most configurations of most systems, that limit is set
higher than a sensible architecture would attempt to use.

-- chris
 
C

Chris Brat

Chris said:
Not to mention that in most configurations of most systems, that limit is set
higher than a sensible architecture would attempt to use.

-- chris

Hi,

Both good points.

Unfortunately I worked on an existing application that did run out of
file-handles and caused the environent to sieze.

To manage the problem the support team wrote a script to periodically
check the number of open filehandles and when a pre-defined limit was
reached the application would reboot, to prevent the environment
siezing.

I don't think the problem was ever actually resolved.

Regards,
Chris
 
P

puzzlecracker

Chris said:
Hi,

Both good points.

Unfortunately I worked on an existing application that did run out of
file-handles and caused the environent to sieze.

To manage the problem the support team wrote a script to periodically
check the number of open filehandles and when a pre-defined limit was
reached the application would reboot, to prevent the environment
siezing.

I don't think the problem was ever actually resolved.

Regards,
Chris
OK, is it possible in Java to find when max fd when is being (or about
to be ) reached? how? what is a good design for it?

thanks
 
G

Gordon Beaton

OK, is it possible in Java to find when max fd when is being (or
about to be ) reached? how? what is a good design for it?

There is no general way to know how many file descriptors you've got
open, except by keeping track of every Socket, ServerSocket, and
FileInputStream you open (and I've probably missed some), and knowing
when library methods open these things for you (as Runtime.exec()
does).

On Linux you can cheat a little by counting the files in
/proc/self/fd, and you can check the limit with "ulimit -n".

But this seems to like a lot of trouble, I don't see the point of
knowing that they will "soon" run out, because you'll get an exception
when you attempt to exceed the limit anyway (so why stop sooner).

What real problem are you trying to solve?

/gordon
 
C

Chris Uppal

Chris said:
Unfortunately I worked on an existing application that did run out of
file-handles and caused the environent to sieze.

To manage the problem the support team wrote a script to periodically
check the number of open filehandles and when a pre-defined limit was
reached the application would reboot, to prevent the environment
siezing.

Good sound support engineering ;-)

In fact, if you have a leak in code you can't get at, such as the OS or a
third-party library, then there may be nothing you can do except restart at
intervals. I think it's a good idea for long-running processes to restart
themselves automatically from time to time -- just as a precaution.

-- chris
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top