【问题标题】:java.io.IOException: Invalid argument and Too many open filesjava.io.IOException:无效的参数和打开的文件太多
【发布时间】:2011-05-19 23:55:45
【问题描述】:

我使用下载种子的库。但是今天发生了一些错误(日志):

Could not create temp files
java.io.IOException: Invalid argument
    at java.io.RandomAccessFile.setLength(Native Method)
    at jBittorrentAPI.DownloadManager.checkTempFiles(DownloadManager.java:261)
    at jBittorrentAPI.DownloadManager.(DownloadManager.java:108)
    at torrentstealer.DownloadTorrent.run(DownloadTorrent.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Could not create temp files
java.io.IOException: Invalid argument
    at java.io.RandomAccessFile.setLength(Native Method)
    at jBittorrentAPI.DownloadManager.checkTempFiles(DownloadManager.java:261)
    at jBittorrentAPI.DownloadManager.(DownloadManager.java:108)
    at torrentstealer.DownloadTorrent.run(DownloadTorrent.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Could not create temp files
java.io.IOException: Invalid argument
    at java.io.RandomAccessFile.setLength(Native Method)
    at jBittorrentAPI.DownloadManager.checkTempFiles(DownloadManager.java:261)
    at jBittorrentAPI.DownloadManager.(DownloadManager.java:108)
    at torrentstealer.DownloadTorrent.run(DownloadTorrent.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Could not create temp files
java.io.IOException: Invalid argument
    at java.io.RandomAccessFile.setLength(Native Method)
    at jBittorrentAPI.DownloadManager.checkTempFiles(DownloadManager.java:261)
    at jBittorrentAPI.DownloadManager.(DownloadManager.java:108)
    at torrentstealer.DownloadTorrent.run(DownloadTorrent.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Could not create temp files
java.io.FileNotFoundException: /root/TorrentStealer/downloads/MAME - Update ROMs (v0.141 to v0.141u2)/lah_l104.zip (Too many open files)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.(RandomAccessFile.java:212)
    at jBittorrentAPI.DownloadManager.checkTempFiles(DownloadManager.java:260)
    at jBittorrentAPI.DownloadManager.(DownloadManager.java:108)
    at torrentstealer.DownloadTorrent.run(DownloadTorrent.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Could not create temp files
java.io.FileNotFoundException: /root/TorrentStealer/downloads/Degrassi.Junior.High.S01-S03.INTERNAL.DVDRip.XviD-FFNDVD/Degrassi.Junior.High.S01.INTERNAL.DVDRip.XviD-FFNDVDDegrassi.Junior.High.S01E10.INTERNAL.DVDRip.XviD-FFNDVDdegrassi.junior.high.s01e10.internal.dvdrip.xvid-ffndvd.r10 (Too many open files)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.(RandomAccessFile.java:212)
    at jBittorrentAPI.DownloadManager.checkTempFiles(DownloadManager.java:260)
    at jBittorrentAPI.DownloadManager.(DownloadManager.java:108)
    at torrentstealer.DownloadTorrent.run(DownloadTorrent.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Could not create temp files
java.io.FileNotFoundException: /root/TorrentStealer/downloads/MAME - Update ROMs (v0.141 to v0.141u2)/lah_l108.zip (Too many open files)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.(RandomAccessFile.java:212)
    at jBittorrentAPI.DownloadManager.checkTempFiles(DownloadManager.java:260)
    at jBittorrentAPI.DownloadManager.(DownloadManager.java:108)
    at torrentstealer.DownloadTorrent.run(DownloadTorrent.java:65)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
以此类推,日志继续。我发现该文件存在,这是发生错误的代码的一部分:
    /**
     * Check the existence of the files specified in the torrent and if necessary,
     * create them
     *
     * @return int
     * @todo Should return an integer representing some error message...
     */
    public synchronized int checkTempFiles() {
        String saveas = Constants.SAVEPATH; // Should be configurable
        if (this.nbOfFiles > 1)
            saveas += this.torrent.saveAs + "/";
        else
        {
            this.torrent.saveAs = this.torrent.name.get(0).toString();
        }
        new File(saveas).mkdirs();
        for (int i = 0; i < this.nbOfFiles; i++) {
            File temp = new File(saveas + ((String) (this.torrent.name.get(i))));
            try {
                this.output_files[i] = new RandomAccessFile(temp, "rw");
                this.output_files[i].setLength((Integer)this.torrent.length.get(
                        i));
            } catch (IOException ioe) {
                System.err.println("Could not create temp files");
                ioe.printStackTrace();
            }
        }
        return 0;
    }
在这里调用:
        // запуск закачки
        dm.startListening(49152, 65535);
        dm.startTrackerUpdate();

    while(true)
    {
        if(dm.isComplete())
        {
            break;
        }

        try
        {
            Thread.sleep(100);
        }
        catch(InterruptedException ex)
        {
            Logger.getLogger(DownloadTorrent.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    dm.stopTrackerUpdate();
    dm.closeTempFiles();</code></pre>

这里调用了上面的代码: ex = Executors.newFixedThreadPool(3);

for(字符串 torrentFile : torrentFiles) { ex.submit(new DownloadTorrent("/root/TorrentStealer/torrents/" + torrentFile)); } // ожидание завершения потоков загрузки файлов торрентов ex.shutdown(); 尝试 { ex.awaitTermination(30, TimeUnit.MINUTES); } 捕捉(InterruptedException ex1) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex1); } ex.shutdownNow();

这里是修复«打开的文件太多»的描述: http://wiki.magnolia-cms.com/display/WIKI/Too+Many+Open+Files

我设置了 999999 max files,修复了一段时间的错误,然后设置了 9999999 进行测试,并没有真正起作用,总是发生错误。服务器重启后,运行后一瞬间不能打开9999999个文件!

所以男人们我不知道该怎么办。你能帮帮我吗?

以下是发生异常时打开文件的计数: T217:~# lsof | wc -l 结果是:2708

【问题讨论】:

  • 我不知道您为什么必须发布所有这些重复的相同错误。完全浪费带宽。

标签: java exception io


【解决方案1】:

堆栈跟踪在接受long 数据类型的 java.io.RandomAccessFile.setLength() 处指示错误。在您的代码中,您将this.torrent.length.get(i) 的结果转换为整数。

does this.torrent.length.get(i) 返回什么?

当您将数据从返回值转换为整数时,可能会出现一些缩小问题,可能会返回导致RandomAccessFile.setLength() 出错的负数。

【讨论】:

  • 检查了 this.torrent.name.get(i),这是文件之一的变量值:-16929275。下一个长度在任何地方都可以。这是解析种子并设置值的代码: this.torrent.length.add(((Long) ((Map) multFiles.get(i)).get("length")).intValue());还有很多打开的文件有什么问题?
  • 哦,是的,setLength 发生时的长度总是有符号的。当«许多文件»异常发生时,总是无符号且正确。
  • Cast to Long 修复了第一个问题。但是如何处理«许多打开的文件»?
  • 这是当前异常日志:pastebin.com/XDY1diiz。通常长度是 15000000 这也是正确的吗?
  • 好的,我想问题已经解决了。 ulimit -n 65535 在 .bashrc 中修复了这个问题。但是程序存在一些问题,cracks无一例外。将调试它。
【解决方案2】:
java.io.IOException: Invalid argument
    at java.io.RandomAccessFile.setLength(Native Method)

您传递的是负值。将值转换为 long 而不是 Integer。

java.io.FileNotFoundException: /root/TorrentStealer/downloads/MAME - Update ROMs (v0.141 to v0.141u2)/lah_l104.zip (Too many open files)

您没有关闭文件。您打开的每个需要关闭的资源都必须关闭,在 finally 块中以确保没有逃逸。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 2014-09-21
    • 2013-04-02
    • 2015-12-17
    • 2010-10-21
    相关资源
    最近更新 更多