Ulimit vs file-max

limitopen filesulimit

Could someone explain limit on open files in linux? The problem is that one of my applications if reporting "Too many open files".

I have

ulimit -n
1024

but

cat /proc/sys/fs/file-max
6578523

and

cat /proc/sys/fs/file-nr
1536

So I already have 1536 > 1024. What is ulimit -n then? This is very confusing.

Best Answer

  • ulimit shows the per-process maximum. The two files under /proc shows system-wide numbers.


    From ServerFault:

    file-max is the maximum File Descriptors (FD) enforced on a kernel level, which cannot be surpassed by all processes without increasing. The ulimit is enforced on a process level, which can be less than the file-max.

    From http://www.linuxvox.com/post/what-are-file-max-and-file-nr-linux-kernel-parameters/:

    What is the file-max parameter and what should we tune it to? The linux documentation definition is that file-max denotes the maximum number of filehandles that the Linux kernel will allocate.

    [...]

    How do I know if I’m getting close to hitting this limit on my server? Run the command: cat /proc/sys/fs/file-nr. This will return three values, denote the number of allocated file handles, the number of allocated but unused file handles, and the maximum number of file handles. Note that file-nr IS NOT a tunable parameter. It is informational only. On my server, this returns: 3488 0 793759. This means that currently, my server has only allocated 3488 of the 793,759 allocation limit and is in no danger of hitting this limit at this time.