Saturday, July 18, 2009

File descriptor limits on GNU/Linux


The application I'm working on at the moment (a document management system - more about that later) requires the ability to handle a large number of file-system files. In PHP, we make use of the DirectoryIterator class in the SPL and the ZipArchive library for iterating over directories and creating archives.

Linux limits the number of file descriptors that any one process (in this case PHP) may open. By default, in Debian and Ubuntu, this is set to 1024 - a number we frequently exceed. As a result we were encountering numerous "Too many open files" errors in PHP.

The file descriptor limit on your system can be found in the output of
ulimit -a
:

[rob@rob-desktop] /# ulimit -a
core file size (blocks) 1000000
data seg size (kbytes) unlimited
file size (blocks) unlimited
max memory size (kbytes) unlimited
stack size (kbytes) 8192
cpu time (seconds) unlimited
max user processes unlimited
pipe size (512 bytes) 8
open files 1024

To increase the limit, edit /etc/security/limits.conf and add the following lines:

user soft nofile limit
user hard nofile limit

Where
user
is the username (wildcard * accepted) and
limit
is the new file limit.

Log out and back in and the new file descriptor limit will be set.


1 comment:

kiran aghor said...

hello Rob,
This is regarding WEB2BB framework (http://chopwoodfetchwater.com/). It says it requires php >=5.3.
I read your comments on a blog - http://giorgiosironi.blogspot.com/2009/10/optimizing-php-application-in-5-minutes.html.
Could u please provide me the source code of your version of WEB2BB which u made compatible with php 5.x? My email id is kiranaghor a..t. gmail.com

Post a Comment