Monday, October 05, 2009

Tutorial to fix error: Too Many Open Files in Linux


Last week tomcat log file report many error about "Too many open files" when it has high traffic. Some in catalina_log say
2007-04-07 16:13:40 HttpProcessor[80][272] Starting background thread
2007-04-07 16:13:40 HttpConnector[80] accept: Too many open files

and here is from localhost_log
2007-04-07 16:13:40 StandardWrapperValve[myservlet]: Servlet.service() for servlet myservlet threw exception /home/log/mylog_070407.log (Too many open files)

This is because too many file descriptors're opened by tomcat. File descriptor can be limited in both system level and shell level.

To check maximum number of fd in system type 'cat /proc/sys/fs/file-max'. In my case it is 65536(someone said it should set to 200000). Tomcat error when try to open socket number 272 so I think 65536 is ok for me for now. Anyway if u want to set it add 'fs.file-max = 200000' to /etc/sysctl.conf
pnix@pnix-a7:~$ cat /proc/sys/fs/file-max

next go check shell level 'ulimit -n' give me 1024. This is reasonable b'coz I have three servlet each need about 3-4 fds per request. So I change this limit by add
* soft nofile 2048
* hard nofile 2048

in file /etc/security/limits.conf

To get the total number of handles that are used by process xxx
lsof -p xxx|wc -l


On Linux (and possibly other Unix-based systems) it's possible to try and fire up an Eclipse-based product and see an error like the following in your console or log:

Error: "An internal error occurred during: "MyEclipse Memory Monitor".
/home/user/Genuitec/Common/binary/ /home/user/Genuitec/Common/binary/ cannot open shared object file: Too many open files"

The reason behind this is that on Unix based systems the kernel AND the shell (e.g. "console") can both be set with individual limits on how many file descriptors can be open.

Naturally the kernel-level limit will be enforced system-wide, and this limit is handled by the fs.file-max argument in the /etc/sysctl.conf file.

TIP: You can check the current setting by using the command cat /proc/sys/fs/file-max

It is recommended that you set the kernel file descriptor limit to 200k, you can do that by editing the /etc/sysctl.conf file and adding the following line to it:


Now that you have done this, it's still possible that the individual shell file descriptor limit is small enough that when MyEclipse (or Eclipse) starts up, they are still opening too many file descriptors as limited by the shell.

To increase that value, you will want to edit the /etc/security/limits.conf file and increasing the file handle limits with the following lines:

* hard nofile 65535
* soft nofile 65535

TIP: You can check the current setting by using the command ulimit -n

Upping the shell limits to 65k seems to be enough in most cases. When both these tips are used together, you must restart your computer to ensure the changes have taken effect.

With both of these changes in place, your days of "Too many files open" with MyEclipse on Linux should be long gone!

A big thanks goes out to user timatracy and the Linux & Java tips blog for the solutions!

Coba Yahoo! Messenger 10 Beta yang baru
Kini dengan update real-time, panggilan video, dan banyak lagi!