I\'m getting this exception in Java:
java.io.FileNotFoundException: (Too many open files)
I\'m looking for the ways to eliminate this prob
You didn't say which OS you are running on, but if you are running on Linux you can use the lsof command
lsof -p <pid of jvm>
That will list all the files opened by the JVM. Or if you are running on Windows you can Process Explorer which will show all the open files for all the processes.
Doing this will hopefully allow you to narrow down which bit of the code is keeping the files open.
Since you are on Linux, I'd suggest, that you check the /proc-Filesystem. Inside proc, you will find a folder with the PID of your process containing a folder calld 'fd'. If your process id is 1234, the path is be
/proc/1234/fd
Inside that folder, you will find links to all opened files (do a 'ls -l'). Usually, you can tell by the filename which library / code might open and not close the file.
If you are on MacOS
sudo launchctl limit maxfiles <hard> <soft>
sudo launchctl limit maxfiles 1024 200000
So, full answer (I combined answers from @phisch and @bramp).
If you want to check all processes, you should use sudo
. Also it's nice to save result to file - lsof is not cheap + this file could be useful for further investigation.
sudo lsof > lsof.log
Show bad guys (with UPDATE from @Arun's comment):
cat lsof.log | awk '{print $1 " " $2 " " $5}' | sort | uniq |awk '{ print $2 " " $1; }' | sort -rn | uniq -c | sort -rn | head -5
2687 114970 java
131 127992 nginx
109 128005 nginx
105 127994 nginx
103 128019 nginx
Save list of file descriptors to file as well:
sudo ls -l /proc/114970/fd > fd.log
Show top open files:
cat fd | awk '{ print $11 }' | sort -rn | uniq -c | sort -rn | head -n20
You can change the limit of opened files by adding the following to /etc/security/limits.conf
:
* soft nofile 2048 # Set the limit according to your needs
* hard nofile 2048
Then you can reload the configuration using sysctl -p
on the shell. Check this article.
Just for completeness you can verify what is the current limit for opened files using: ulimit -n