为什么我会收到“打开的文件太多”的信息错误?
我发现自己必须在 Groovy 脚本中显式调用 System.gc() 以防止出现如下错误。为什么垃圾收集器不为我做这件事?我可以做些什么来使其进行垃圾收集以防止这些错误(也许是 JAVA_OPTS)?
Caught: java.util.concurrent.ExecutionException: org.codehaus.groovy.runtime.InvokerInvocationException: java.io.IOException: Cannot run program "ls": java.io.IOException: error=24, Too many open files
at groovyx.gpars.GParsPool.runForkJoin(GParsPool.groovy:305)
at UsageAnalyzer$_run_closure2_closure6.doCall(UsageAnalyzer.groovy:36)
at groovyx.gpars.GParsPool$_withExistingPool_closure1.doCall(GParsPool.groovy:170)
at groovyx.gpars.GParsPool$_withExistingPool_closure1.doCall(GParsPool.groovy)
at groovyx.gpars.GParsPool.withExistingPool(GParsPool.groovy:169)
at groovyx.gpars.GParsPool.withPool(GParsPool.groovy:141)
at groovyx.gpars.GParsPool.withPool(GParsPool.groovy:117)
at groovyx.gpars.GParsPool.withPool(GParsPool.groovy:96)
at UsageAnalyzer$_run_closure2.doCall(<removed>)
at UsageAnalyzer.run(<removed>)
该堆栈跟踪来自并行程序,但它也发生在顺序程序中。
I find myself having to explicitly call System.gc() in my Groovy scripts to prevent errors like the one below. Why doesn't the garbage collector do this for me? Is there something I can do to cause it to garbage collect to prevent these errors (maybe JAVA_OPTS)?
Caught: java.util.concurrent.ExecutionException: org.codehaus.groovy.runtime.InvokerInvocationException: java.io.IOException: Cannot run program "ls": java.io.IOException: error=24, Too many open files
at groovyx.gpars.GParsPool.runForkJoin(GParsPool.groovy:305)
at UsageAnalyzer$_run_closure2_closure6.doCall(UsageAnalyzer.groovy:36)
at groovyx.gpars.GParsPool$_withExistingPool_closure1.doCall(GParsPool.groovy:170)
at groovyx.gpars.GParsPool$_withExistingPool_closure1.doCall(GParsPool.groovy)
at groovyx.gpars.GParsPool.withExistingPool(GParsPool.groovy:169)
at groovyx.gpars.GParsPool.withPool(GParsPool.groovy:141)
at groovyx.gpars.GParsPool.withPool(GParsPool.groovy:117)
at groovyx.gpars.GParsPool.withPool(GParsPool.groovy:96)
at UsageAnalyzer$_run_closure2.doCall(<removed>)
at UsageAnalyzer.run(<removed>)
This stack trace is from a parallel program but it happens in sequential programs as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用 groovy 时,您可以使用方便的方法,例如
File.withReader()
、File.withWriter()
、File.withInputStream()< /code>,
InputStream.withStream()
确保资源完全关闭。这比使用 Java 的try ..finally
习惯用法要简单,因为不需要显式调用close()
,或者在try
之外声明变量代码>块。例如从文件中读取。
As you're using groovy, you can use the convenient methods such as
File.withReader()
,File.withWriter()
,File.withInputStream()
,InputStream.withStream()
to ensure resources get closed cleanly. This is less cumbersome than using Java'stry .. finally
idiom, as there's not need to explicitly callclose()
, or declare a variable outside thetry
block.e.g. to read from a file.
一定要查找您打开文件或流的任何位置,并确保关闭它们。像这样包裹它们通常是有益的:
Definitely look for any place you open files or streams and make sure you close them. It's often beneficial to wrap them like this: