Friday, 14 April 2017

Too many open files (java.io.IOException: Cannot run program "chmod": error=24)

One week ago, on production we got java.io.IOException: Cannot run program "chmod": error=24 Too many open files.

Cause
         Basically we are copying files form Zip file to a folder on user action. And  we got one zip which has 15000 files in it.  we  open stream of input file and  output file , and copying from one stream to output file stream. Bad luck we forgot to close these streams (two stream, Input and Output for every file in Zip) . Ultimately there are 15000 * 2 = 30000 file descriptor is opened by single process.

Debug
         After some investigation we found, we have limit on number of File descriptor open by single process on production machine (Suse Linux).  The system command ulimit is great usefully. We have found configuration in file ~/etc/security/limits.conf .

We have found following setting in limits.conf file
* hard nofile 100000

* soft nofile 100000

Which indicate max and min number of file descriptor open by single process. But as per code we have opened 30000 file descriptor and this how we got Exception.

Solution

  • First and obvious solution is close stream after task done.
  • Second is to increase no. of file descriptor per process, which is not accepted by our Linux administrator. 
Java(1.7) Try with Resource Come in Handy to Auto close resource 
  • Best way to use java 1.7 new feature, try resource with which will close input stream, we don't have to close it manually.
  • Also it suppress any Exception accord during resource close.