2

I'm trying to copy all the image files on an SD card (connected to a rooted Android device running 4.0.4) to a Hard Drive connected to the same device. I'm new to Android programming so forgive me if the question's answer is obvious.

I use the following method, which is called from the onHandleIntent(Intent intent) of a service, to to the copying:

private void copyImages(File sourceDirectory, File destinationDirectory) throws IOException {
    int count = 0;
    IOFileFilter imageFilter = new ImageFilter();
    IOFileFilter visibleDirectoryFilter = new VisibleDirectoryFilter();

    long startTime = System.nanoTime();
    if (sourceDirectory.exists() && destinationDirectory.exists()) {
        Collection<File> fileList = FileUtils.listFiles(sourceDirectory, imageFilter, visibleDirectoryFilter);
        for (File image : fileList) {               
            FileUtils.copyFileToDirectory(image, destinationDirectory);
            count++;
        }
    }

    long totalTime = System.nanoTime() - startTime;
    Toast.makeText(this, count + " files copied in " + (totalTime) + " nanos.", Toast.LENGTH_LONG).show();
}

However, when I run this, after 36 images are copied, I start continuously getting GC_CONCURRENT freed aK, b% free cK/dK, paused ems+fms where a, b, c , d, e and f are different numbers. I get this non-stop for a long time (as long as I've waited) without any other images being copied.

I understand (I think I do anyway) that this is because the heap is full and Dalvik is trying to free up memory. But, I'm not sure how I can stop this from happening, the program should be able to copy many more (possibly thousands) images. Perhaps the apache commons FileUtils class is too heavy for Android in which case is there another Android-friendly library for neat file handling or must I write my own simple one? Or maybe I'm going wrong elsewhere.

Any help is much appreciated.

Thanks in advance.

4

1 回答 1

-1

这很可能不是问题。GC 是应用程序处理的正常部分。事实上,我敢打赌,如果您的应用程序正忙于进行缓慢的 IO,那么系统已经决定现在是进行一些清理的好时机。

FileUtils 的源代码当然是开放的。它并不是特别可怕:

http://grepcode.com/file/repo1.maven.org/maven2/commons-io/commons-io/1.4/org/apache/commons/io/FileUtils.java#FileUtils.doCopyFile%28java.io.File% 2Cjava.io.File%2Cboolean%29

唯一可以说“臃肿”的是创建复制缓冲区。默认为 4K: http: //grepcode.com/file/repo1.maven.org/maven2/commons-io/commons-io/1.4/org/apache/commons/io/IOUtils.java#IOUtils.copyLarge%28java。 io.InputStream%2Cjava.io.OutputStream%29

我不会太担心这一点,除非您可以将真正的问题隔离到该代码中。

从性能的角度来看,您可能想要测试 BufferedInputStream/OutputStream 是否在这里提供任何优势。值得怀疑,因为 IOUtils 复制方法也使用了缓冲区。

于 2012-12-03T05:05:07.130 回答