title: Android memory leak when using org.apache.commons.io.FileUtils
body: <p>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.</p>
  
  <p>I use the following method, which is called from the <code>onHandleIntent(Intent intent)</code> of a service, to to the copying:</p>
  
  <pre><code>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() &amp;&amp; destinationDirectory.exists()) {
          Collection&lt;File&gt; 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();
  }
  </code></pre>
  
  <p>However, when I run this, after 36 images are copied, I start continuously getting <code>GC_CONCURRENT freed aK, b% free cK/dK, paused ems+fms</code> where <code>a</code>, <code>b</code>, <code>c</code> , <code>d</code>, <code>e</code> and <code>f</code> are different numbers. I get this non-stop for a long time (as long as I've waited) without any other images being copied.</p>
  
  <p>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.</p>
  
  <p>Any help is much appreciated.</p>
  
  <p>Thanks in advance.</p>