Android:打开文件过多错误 [英] Android: Too many open files error

查看:324
本文介绍了Android:打开文件过多错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下每3秒运行一次的操作.
基本上,它从服务器下载文件,然后每3秒将其保存到本地文件中.
以下代码可以完成一段时间.

I have the following operation which runs every 3 seconds.
Basically it downloads a file from a server and save it into a local file every 3 seconds.
The following code does the job for a while.

public class DownloadTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        downloadCommandFile( eventUrl);
        return null;
    }


}

private void downloadCommandFile(String dlUrl){
    int count;
    try {
        URL url = new URL( dlUrl );
        NetUtils.trustAllHosts();
        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.connect();
        int fileSize = con.getContentLength();
        Log.d(TAG, "Download file size = " + fileSize );
        InputStream is = url.openStream();
        String dir = Environment.getExternalStorageDirectory() + Utils.DL_DIRECTORY;
        File file = new File( dir );
        if( !file.exists() ){
            file.mkdir();
        }

        FileOutputStream fos = new FileOutputStream(file + Utils.DL_FILE);
        byte data[] = new byte[1024];
        long total = 0;

        while( (count = is.read(data)) != -1 ){
            total += count;
            fos.write(data, 0, count);
        }

        is.close();
        fos.close();
        con.disconnect(); // close connection


    } catch (Exception e) {
        Log.e(TAG, "DOWNLOAD ERROR = " + e.toString() );
    }

}

一切正常,但是如果我让它运行5至10分钟,则会出现以下错误.

Everything works fine, but if I leave it running for 5 to 10 minutes I get the following error.

06-04 19:40:40.872:E/NativeCrypto(6320):AppData :: create pipe(2) 失败:打开文件太多06-04 19:40:40.892:E/NativeCrypto(6320): AppData :: create pipe(2)失败:打开的文件太多06-04 19:40:40.892:E/EventService(6320):下载错误= javax.net.ssl.SSLException:无法创建应用程序数据

06-04 19:40:40.872: E/NativeCrypto(6320): AppData::create pipe(2) failed: Too many open files 06-04 19:40:40.892: E/NativeCrypto(6320): AppData::create pipe(2) failed: Too many open files 06-04 19:40:40.892: E/EventService(6320): DOWNLOAD ERROR = javax.net.ssl.SSLException: Unable to create application data

最近两天我一直在做一些研究.
有人建议打开许多连接,例如 https://stackoverflow.com/a/13990490/1503155 但我仍然不知道出了什么问题.
有什么想法可能会导致问题吗?
预先感谢.

I have been doing some researches for the last 2 days.
There are suggestions that they are many connections open, like this one https://stackoverflow.com/a/13990490/1503155 but still I can not figure out what's the problem.
Any ideas what may cause the problem?
Thanks in advance.

推荐答案

我认为您会收到此错误,因为您同时打开了太多文件,这意味着您同时运行了太多异步任务(每个任务异步任务会打开一个文件),如果您说每3秒运行一个新文件,则这是很有意义的.

I think you get this error because you have too many files open at the same times, meaning that you have too many async tasks running in the same time (each async task opens a file), which makes sense if you say that you run a new one every 3 seconds.

您应该尝试使用线程池执行程序来限制同时运行的异步任务的数量.

You should try to limit the number of async task running in the same time using a thread pool executor.

这篇关于Android:打开文件过多错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆