Fixing “Too many open files” error

Often we get “org.apache.tomcat.jni.Error: 24: Too many open files” errors on Apache Tomcat. If this happened then the process will wait for 3 minutes in open state and will fail after that. It can be fixed by increasing the Open file limits on OS limits. By default CentOS is set with 1024 limit.

1. Edit the file /etc/security/limits.conf and add the following lines

*       soft    nofile  8192
*       hard    nofile  65535

2. Edit the file /etc/pam.d/login and add the following lines

session required /lib/security/pam_limits.so

3. Check the limit again by logoff and login

ulimit -a

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 119859
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 8192
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 119859
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

Here we can see the open files is set to 8192 which should be enough but it is best practice to check the open file with lsof and adjust as per requirements.

Hard Disk Performance Testing

To check read speed you can use the following command

hdparm -Tt /dev/sda1

/dev/sda1:
 Timing cached reads:   25292 MB in  2.00 seconds = 12664.26 MB/sec
 Timing buffered disk reads: 432 MB in  3.01 seconds = 143.56 MB/sec

Here 12664.26 MB/sec & 143.56 MB/sec are the read speeds.

DD command will give the write speed of hard drives

dd if=/dev/zero of=/tmp/output bs=1024 count=4096k conv=fdatasync; rm -f /tmp/output
4194304+0 records in
4194304+0 records out
4294967296 bytes (4.3 GB) copied, 165.193 s, 26.0 MB/s

Here 26.0 MB/s is the write speed.

Hope this helps!