紧张的Release阶段中又来了个棘手的Ticket,出错信息是文件存在,但打不开了,单个Process能用的File Descriptor已经到了默认的最大值(1024)。我们可以简单的通过ulimit修改FD限制,增大上限。可Java-based GUI进程并没有打开这么多文件,问题在哪呢?
用lsof观察发现,GUI进程打开发很多PIPE的类型的FD,原来是大量的stdin, stdout和stderr没有关闭。在程序里,GUI会用Runtime.getRuntime().exec()调用一些后台的Utility程序,完成一些工作,每exec()一下就会占用3个PIPE的FD。当外部程序结束时,这些FD也不会被自动关闭。如果GUI进行久一些,FD的上限就可以超过。
改正的方法也很直接–及时的关闭这些不用的Stream,用common.io中的IOUtils关闭这些Steam。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * Close the streams of a process. * @param process the process. */ public static void closeQuitely(Process process) { if (process != null) { // close the streams of stdio, stdout, stderr. IOUtils.closeQuietly(process.getOutputStream()); IOUtils.closeQuietly(process.getInputStream()); IOUtils.closeQuietly(process.getErrorStream()); } } |
把这个函数写在某个finally block里吧。