Sunday, February 24, 2013

Open File Descriptor Limits in Linux


Whenever we mean number of open files limit, we actually mean the file descriptor limit.

Whenever some one asks how many open files are there, usually we just take the output of "lsof | wc -l". But this is not the correct way to do.

So why should we be bothered about open file descriptor limits? 
     Sometime java application server like tomcat may leak file descriptors, that is, will not close the file handles after usage. This may force us to increase the file descriptor(file handle) limit.

Open file descriptor limits are available

  1. For the whole system
  2. Per process wise
  3. Per user wise 
System wide Open File Descriptors(handles) limit

In Linux, there is a limit set in the kernel on how many open file descriptors are allowed on the system.

# cat /proc/sys/fs/file-max
6491402

In this system, 6,491,402 open file descriptors are permitted

How to increase the limit of the file descriptors if "Number Of Maximum Files Was Reached"?

This can be achieved 

a) Using sysctl command. To make the change permanent, add the entries to /etc/sysctl.conf

 Make the following entry in /etc/sysctl.conf 
           fs.file-max = 104854
 
Then run the command
# sysctl -p

          (Or)

b) To change the open file descriptor limit, run the following

 # echo "104854" > /proc/sys/fs/file-max

How many open file descriptors are in actual use in the system?

To find the number of file descriptors that are currently used in the system, run the following

# cat /proc/sys/fs/file-nr
510     0       6491402 

Column1 - 510 - the number of allocated file handles, 
Column2 - 0   - the number of allocated but unused file handles, 
Column3 - 6491042 - the maximum number of file handles (same as /proc/sys/fs/file-max)

No. of file descriptors currently used in the system = Column1 - Column2 (510 - 0) = 510

Process Level Open file descriptors(handles) limit

Linux limits the number of file descriptors that any one process may open; 

To find the default limit per process,

grep -i "max open files" /proc/<PID>/limits

Eg: # grep -i "max open files" /proc/893046/limits
Max open files            1024                 1024                 files

So this process can open 1024 file descriptors

To find the number of file descriptors or handles currently opened by a process

ls -l /proc/<PID>/fd | wc -l

Eg: # ls -l /proc/893046/fd
total 0
lrwx------ 1 root root 64 Feb 17 08:03 0 -> /dev/null
lrwx------ 1 root root 64 Feb 17 08:03 1 -> /dev/null
lrwx------ 1 root root 64 Feb 17 08:03 10 -> socket:[8924389]
l-wx------ 1 root root 64 Feb 17 08:03 2 -> pipe:[8924390]
lr-x------ 1 root root 64 Feb 17 08:03 3 -> eventpoll:[8924395]
lr-x------ 1 root root 64 Feb 17 08:03 4 -> pipe:[8924397]
l-wx------ 1 root root 64 Feb 17 08:03 5 -> pipe:[8924397]
lr-x------ 1 root root 64 Feb 17 08:03 6 -> pipe:[8924398]
l-wx------ 1 root root 64 Feb 17 08:03 7 -> pipe:[8924398]
lrwx------ 1 root root 64 Feb 17 08:03 8 -> socket:[8924387]

User Level Open File descriptor Limit

To find the user level file descriptor limit

 # su - <username>

Show the soft and hard limit using

ulimit -Sn
ulimit -Hn

Eg :
$ ulimit -Hn
1024

$ ulimit -Sn
1024

How to limit open file descriptor limit at user level?

User specific file descriptor limit can be set by editing /etc/security/limits.conf file

Say for user, apache, set the following in /etc/security/limits.conf

apache soft nofile 2048
apache hard nofile 4096

View the change as following

# su - apache
# ulimit -Hn
# ulimit -Sn

No comments:

Post a Comment