File descriptors

Recently we were asked by customers what is the file descriptor limit and why to setup them? If you will run a load testing on your VPS you may be noticed that it ran out of file descriptors. Let look deeply inside the problem.

In the traditional implementation of Unix, file descriptors index into a per-process file descriptor table maintained by the kernel, that in turn indexes into a system-wide table of files opened by all processes, called the file table. This table records the mode with which the file (or other resource) has been opened: for reading, writing, appending, and possibly other modes. It also indexes into a third table called the inode table that describes the actual underlying files. To perform input or output, the process passes the file descriptor to the kernel through a system call, and the kernel will access the file on behalf of the process. The process does not have direct access to the file or inode tables.

On Linux, the set of file descriptors open in a process can be accessed under the path /proc/PID/fd/, where PID is the process identifier.

Very important not to increase the limits, but check your application if it has a memory/file descriptor leak. Use lsof to see all it has open to see if they are valid or not. Don’t try to change your system to work around applications bugs.

How to work with File descriptors?

On centos:

cat /proc/sys/fs/file-max

Gives number of open file descriptors allowed for entire system.

On virtuozzo:

cat /proc/user_beancounters

Tells how many allowed in this container.

There are also limits for each user:

# ulimit -Hn
# ulimit -Sn

And these are good to set to make sure you have enough for apache and mysql.

Looking for a custom solution?

Our technicians can provide you with the best custom made solutions on the market, no matter whether you're a small business or large enterprise.

Get in touch