开发者

why does this error occur? :java.io.FileNotFoundException: /dir/myfile.txt (Too many open files)

I've written an application that runs flawlessly in windows, and throws this error on unix.

Generally, I dont understand why I'm getting this error! I create a single file and .append text to it. After 开发者_开发技巧I've appended a couple thousand lines I got this error...

Any insight would be appreciated.

Nothing in the javadoc listed this (too many files open) error: https://docs.oracle.com/javase/1.5.0/docs/api/java/io/FileNotFoundException.html.

And this post didn't help me: Too many open files: how many are open, what they are, and how many can the JVM open.

Thanks in advance.


Without seeing your code this is guess work, but most likely you are creating a new FileWriter or FileOutputStream object each time you append, but neglect to call close() on these objects, which eventually causes you to run out of file descriptors (an OS level resource that is independant from memory and thus not adequately handled by garbage collection, requiring manual release).

To solve this, either keep a single Stream/Writer around and use it instead of creating a new one each time (this would probably be faster as well), or call close() each time you're done appending.


Perhaps you accidentally open a new file handle for each append? The limits vary with OSs. But how about posting some code - it's hard to guess what's wrong without seeing it.


In linux environments this usually is capped with the flag 'ulimit'. It basically handles at OS level how many files you may open at a time. If you have a lot of dependencies, it might be that they need to be loaded and you can exceed the maximum easiliy that way.

Try setting the ulimit to 4096 before running your app and try again.

Edit: See http://www.faqs.org/docs/securing/x4733.html for more information

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜