简体   繁体   English

使用org.apache.commons.io.FileUtils时Android内存泄漏

[英]Android memory leak when using org.apache.commons.io.FileUtils

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. 我正在尝试将SD卡上的所有图像文件(连接到运行4.0.4的有根Android设备)复制到连接到同一设备的硬盘上。 I'm new to Android programming so forgive me if the question's answer is obvious. 我是Android编程的新手,如果问题的答案很明显,请原谅我。

I use the following method, which is called from the onHandleIntent(Intent intent) of a service, to to the copying: 我使用以下方法,从服务的onHandleIntent(Intent intent)调用到复制:

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. 但是,当我运行此命令时,在复制36张图像后,我开始不断获得GC_CONCURRENT freed aK, b% free cK/dK, paused ems+fms ,其中abcdef是不同的数字。 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. 我了解(无论如何我都认为),这是因为堆已满,而Dalvik试图释放内存。 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? 也许apache commons FileUtils类对于Android来说太重了,在这种情况下,是否还有另一个Android友好的库用于整洁的文件处理,还是我必须编写自己的简单库? Or maybe I'm going wrong elsewhere. 也许我在其他地方出错了。

Any help is much appreciated. 任何帮助深表感谢。

Thanks in advance. 提前致谢。

This is a non-issue most likely. 这是最不可能的。 GC is a normal part of the application processing. GC是应用程序处理的正常部分。 In fact, I'd bet if your app is busy doing slow IO, the system has decided now is a good time to do some cleaning up. 实际上,我敢打赌,如果您的应用程序正忙于执行缓慢的IO,那么系统现在就决定进行清理的好时机。

The source for FileUtils is open, of course. 当然,FileUtils的源是开放的。 Its not particularly scary: 它不是特别可怕:

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 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

The only thing arguably "bloaty" is creating the copy buffer. 唯一可以说是“过大”的事情是创建复制缓冲区。 Default is 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 默认值为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

I would not get too worried about this, unless you can isolate real issues to that code. 除非您可以将实际问题隔离到该代码中,否则我不会对此太担心。

From a performance perspective, you may want to test if BufferedInputStream/OutputStream offer any advantage here. 从性能角度来看,您可能想测试BufferedInputStream / OutputStream是否在此提供了任何优势。 Questionable, as the IOUtils copy method is using a buffer as well. 有问题,因为IOUtils复制方法也使用缓冲区。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Servlet中改善org.apache.commons.io.FileUtils copyInputStreamToFile - How to improve org.apache.commons.io.FileUtils copyInputStreamToFile in Servlet org.apache.commons.io.FileUtils-更改文本编码 - org.apache.commons.io.FileUtils - change text encodig 使用Netbeans 8.0.2,并希望使用org.apache.commons.io.FileUtils; 怎么样? - Using Netbeans 8.0.2 and want to use org.apache.commons.io.FileUtils; how? 引起原因:java.lang.ClassNotFoundException:org.apache.commons.io.FileUtils,maven pom.xml wa - Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils, maven pom.xml wa 我有导入org.apache.commons.io.FileUtils; 在我的代码中,它适用于eclipse但不适用于命令行 - I have import org.apache.commons.io.FileUtils; in my code and it works on eclipse but not in command line Java库,例如org.apache.commons.io.FileUtils,但包含实例方法而不是静态方法 - Java library like org.apache.commons.io.FileUtils but contains instance methods instead of static methods org / apache / commons / io / FileUtils-NoClassDefFoundError - org/apache/commons/io/FileUtils - NoClassDefFoundError 在 org.apache.commons.io.FileUtils.copyURLToFile 上设置超时? - Set timeout on org.apache.commons.io.FileUtils.copyURLToFile? org.apache.commons.io.FileUtils.readFileToString 和文件路径中的空白 - org.apache.commons.io.FileUtils.readFileToString and blanks in filepath 缺少方法 org.apache.commons.io.FileUtils.forceMkdirParent? - missing methode org.apache.commons.io.FileUtils.forceMkdirParent?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM