使用AsyncTask下载14'000个小图像时Android,内存泄漏

时间:2013-06-04 08:27:09

标签: android optimization garbage-collection android-asynctask download

我目前正在项目中使用Phonegap,我需要下载数千张图片。 从现在开始,它直接在Javascript中使用Phonegap的API完成。但是需要花费相当长的时间(5到10分钟之间)。

然后我决定用纯Java做这个任务。它运作良好!但我认为它可能会更快。

下载~1k图像后,应用程序开始每4-5秒抛出一次“GC_CONCURRENT FREED”并完全停止下载1-2次。我在Eclipse中的网络统计就像一个过山车。

在这里你们都在等待:

我的下载课程:

public class DownloadImage extends AsyncTask<String, Integer, Boolean>
{   
    protected static String destination = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.test.test/cache/pic_test/";
    protected static Context context;
    protected static String taskName;
    protected static ArrayList<String> urls;
    protected static int totalSize;

    public DownloadImage(Context context, String taskName, ArrayList<String> urls) {
        DownloadImage.context = context;
        DownloadImage.taskName = taskName;
        DownloadImage.urls = urls;
        DownloadImage.totalSize = urls.size();
    }

    public DownloadImage() {}

    @Override
    protected Boolean doInBackground(String... params) {
        String urlstring, filename;
        URL url;
        HttpURLConnection urlConnection;
        File tmpFile;
        FileOutputStream fileOutput;
        InputStream inputStream;
        byte[] buffer = new byte[8192];
        int bufferLength;

        try {
            while(DownloadImage.urls.size() > 0) {
                try {
                    urlstring = DownloadImage.urls.remove(0);
                    url = new URL(urlstring);
                    filename = urlstring.substring(urlstring.lastIndexOf("/")+1, urlstring.length());
                    urlConnection = (HttpURLConnection) url.openConnection();

                    //configuration de la connexion
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setDoOutput(true);
                    urlConnection.setConnectTimeout(5000);
                    urlConnection.setReadTimeout(5000);
                    urlConnection.connect();

                    tmpFile = new File(DownloadImage.destination+filename + ".temp");
                    fileOutput = new FileOutputStream(tmpFile);
                    inputStream = urlConnection.getInputStream();

                    bufferLength = 0;
                    //lecture du flux
                    Log.d("DownloadFile","Téléchargement en cours "+filename);
                    while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                        fileOutput.write(buffer, 0, bufferLength);
                    }
                    fileOutput.close();
                    inputStream.close();
                    ((TestActivity)(DownloadImage.context)).sendJavascript("Application.testCallback('"+DownloadImage.totalSize+"', '"+(DownloadImage.totalSize - DownloadImage.urls.size() + 1)+"')");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch(IndexOutOfBoundsException e) {}
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if(DownloadImage.context instanceof TestActivity) {
            Log.d("test_flo", "no task left in queue");
            //((TestActivity) this.context).finishTask(this.taskName);
        }
    }
}

我的主要活动:

private class AndroidFunction {
    @SuppressWarnings("unused")
    private WebView mAppView;
    @SuppressWarnings("unused")
    private DroidGap mGap;

    public AndroidFunction(DroidGap gap, WebView view) {
        mGap = gap;
        mAppView = view;
    }

    @SuppressWarnings("unused")
    public String test() {          
        ArrayList<String> urls = new ArrayList<String>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(getAssets().open("testpic.txt")));
            String line;
            while ((line = br.readLine()) != null) {
                urls.add(line);
            }
            br.close();
        } catch(Exception e) {}
        new DownloadImage(TestActivity.this, "test", urls).execute();
        new DownloadImage().execute();
        new DownloadImage().execute();
        new DownloadImage().execute();
        new DownloadImage().execute();
        return "";
    }
}

正如你所看到的,我试图尽可能少地使用较少的对象,在每个循环之间重用它们。 你有没有看到为什么它还在抛出“GC_CONCURRENT FREED”的原因?

提前感谢您的答案!

0 个答案:

没有答案