Right now, I know how to:
- find open files limit per process:
ulimit -n - count all opened files by all processes:
lsof | wc -l - get maximum allowed number of open files:
cat /proc/sys/fs/file-max
My question is: Why is there a limit of open files in Linux?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The reason is that the operating system needs memory to manage each open file, and memory is a limited resource – especially on embedded systems.
As root user you can change the maximum of the open files count per process (via ulimit -n) and per system (e.g. echo 800000 > /proc/sys/fs/file-max).
Method 2
Please note that lsof | wc -l sums up a lot of duplicated entries (forked processes can share file handles etc). That number could be much higher than the limit set in /proc/sys/fs/file-max.
To get the current number of open files from the Linux kernel’s point of view, do this:
cat /proc/sys/fs/file-nr
Example: This server has 40096 out of max 65536 open files, although lsof reports a much larger number:
# cat /proc/sys/fs/file-max 65536 # cat /proc/sys/fs/file-nr 40096 0 65536 # lsof | wc -l 521504
Method 3
I think it’s largely for historical reasons.
A Unix file descriptor is a small int value, returned by functions like open and creat, and passed to read, write, close, and so forth.
At least in early versions of Unix, a file descriptor was simply an index into a fixed-size per-process array of structures, where each structure contains information about an open file. If I recall correctly, some early systems limited the size of this table to 20 or so.
More modern systems have higher limits, but have kept the same general scheme, largely out of inertia.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0