How do linux file descriptor limits work?
You want to look at /proc/sys/fs/file-max instead
From recent linux/Documentation/sysctl/fs.txt:
file-max & file-nr:
The kernel allocates file handles dynamically, but as yet it
doesn't free them again.
The value in file-max denotes the maximum number of file-
handles that the Linux kernel will allocate. When you get lots
of error messages about running out of file handles, you might
want to increase this limit.
Historically, the three values in file-nr denoted the number of
allocated file handles, the number of allocated but unused file
handles, and the maximum number of file handles. Linux 2.6 always
reports 0 as the number of free file handles -- this is not an
error, it just means that the number of allocated file handles
exactly matches the number of used file handles.
Attempts to allocate more file descriptors than file-max are
reported with printk, look for "VFS: file-max limit
reached".
EDIT: the underlying error is probably not the system running out of global filedescriptors, but just your process. It seems likely that the problem is the maximum size limit of select.
About limiting the number of file descriptors
/proc/sys/fs/file-max
takes precedence over any ulimit settings in the shell.
More over /proc/sys/fs/file-max
is the total number of FD open for ALL processes on given machine.
ulimit
settings are per process, so any new process started will have given limit (unless the total form file-max
is exceeded in the system).
How can we limit file descriptor to always be less than 1024?
You don't want to do this. Linux always uses the lowest possible file descriptor. So if you get file descriptor 1024 then it means file descriptors 0 up to 1023 were all used already.
If you make Linux only use file descriptors 0-1023, then your program still won't work, because instead of getting file descriptor 1024, you'll get an error saying there aren't any more file descriptors it can use.
You should:
- Make sure your program closes file descriptors when it's done with them.
Maybe the reason descriptors 0-1023 were all used is because you forgot to close them. So many sure your program closes them.
If they were real file descriptors that your program was actually using, not just ones you forgot to close, then continue to step 2... - Use
poll
instead ofselect
, like the document says. It is not a difficult change. - Consider using
epoll
, which is more efficient thanselect
when you are polling a lot of file descriptors at the same time. However, it is much more complicated.
What's the impact of large number of file descriptors on Java applications
Operating systems have a limit on the number of files that can be concurrently open by any one process. The default for most distributions is only 1024 files. Each open file also has an associated file-descriptor. Socket connections are treated like files and they use file descriptor, and are therefore subject to the same resource limits.
You can verify or change the maximum limit by the command
ulimit
.You can also view value of the MBean Attibutes -
MaxFileDescriptorCount & OpenFileDescriptorCount by running the JMX tool- JConsole.When OpenFileDescriptorCount is less than MaxFileDescriptorCount
your application works fine, otherwise you would getjava.io.IOException: Too many open files
which causes malfunctioning of your application .Normally for an application the count of the FD(File Descriptor)
goes up/down to certain level. But it should be within
MaxFileDescriptorCount.
Is the value of a Linux file descriptor always smaller than the open file limits?
Yes, the values are limited to the range from 0 to one less than the current limit as returned by getrlimit().
From the getrlimit() man page:
RLIMIT_NOFILE
Specifies a value one greater than the maximum file descriptor number
that can be opened by this process. Attempts (open(2), pipe(2),
dup(2), etc.) to exceed this limit yield the error EMFILE.
(Historically, this limit was named RLIMIT_OFILE on BSD.)
From the Open Group Base Specification:
RLIMIT_NOFILE
This is a number one greater than the maximum value that the system may assign to a newly-created descriptor. If this limit is
exceeded, functions that allocate a file descriptor shall fail with
errno set to [EMFILE]. This limit constrains the number of file
descriptors that a process may allocate.
Related Topics
How to Parse Command Line Options in Bash Shell
Why Do My Results Different Following Along the Tiny Asm Example
Shell Script for Multithreading a Process
Emulating Slurm on Ubuntu 16.04
Systemd with Multiple Execstart
How to Diff Directories Over Ssh
What's the Difference Between Event-Driven and Asynchronous? Between Epoll and Aio
Bash Foreach Loop Works Differently When Executed from .Sh File
(Master) at End of Terminal Prompt
Installed Go Binary Not Found in Path on Alpine Linux Docker
Turning Multiple Lines into One Comma Separated Line
Quick Unix Command to Display Specific Lines in the Middle of a File
Different Results Between Ps Aux and 'Ps Aux' Inside a Script
How to Stop a Running R Command in Linux Other Than with Ctrl + C