title: Java not garbage collecting memory
body: <p>I am reading a very large file and extracting some small portions of text from each line. However at the end of the operation, I am left with very little memory to work with. It seems that the garbage collector fails to free memory after reading in the file.</p>
  
  <p>My question is: Is there any way to free this memory? Or is this a JVM bug?</p>
  
  <p>I created an SSCCE to demonstrate this. It reads in a 1 mb (2 mb in Java due to 16 bit encoding) file and extracts one character from each line (~4000 lines, so should be about 8 kb). At the end of the test, the full 2 mb is still used!</p>
  
  <p>The initial memory usage:</p>
  
  <pre><code>Allocated: 93847.55 kb
  Free: 93357.23 kb
  </code></pre>
  
  <p>Immediately after reading in the file (before any manual garbage collection):</p>
  
  <pre><code>Allocated: 93847.55 kb
  Free: 77613.45 kb (~16mb used)
  </code></pre>
  
  <p>This is to be expected since the program is using a lot of resources to read in the file.</p>
  
  <p>However then I garbage collect, but not all the memory is freed:</p>
  
  <pre><code>Allocated: 93847.55 kb
  Free: 91214.78 kb (~2 mb used! That's the entire file!)
  </code></pre>
  
  <p>I know that manually calling the garbage collector doesn't give you any guarantees (in some cases it is lazy). However this was happening in my larger application where the file eats up almost all available memory, and causes the rest of the program to run out of memory despite the need for it. This example confirms my suspicion that the excess data read from the file is not freed.</p>
  
  <p>Here is the SSCCE to generate the test:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  
  public class Test {
      public static void main(String[] args) throws Throwable {
          Runtime rt = Runtime.getRuntime();
  
          double alloc = rt.totalMemory()/1000.0;
          double free = rt.freeMemory()/1000.0;
  
          System.out.printf("Allocated: %.2f kb\nFree: %.2f kb\n\n",alloc,free);
  
          Scanner in = new Scanner(new File("my_file.txt"));
          ArrayList&lt;String&gt; al = new ArrayList&lt;String&gt;();
  
          while(in.hasNextLine()) {
              String s = in.nextLine();
              al.add(s.substring(0,1)); // extracts first 1 character
          }
  
          alloc = rt.totalMemory()/1000.0;
          free = rt.freeMemory()/1000.0;
          System.out.printf("Allocated: %.2f kb\nFree: %.2f kb\n\n",alloc,free);
  
          in.close();
          System.gc();
  
          alloc = rt.totalMemory()/1000.0;
          free = rt.freeMemory()/1000.0;
          System.out.printf("Allocated: %.2f kb\nFree: %.2f kb\n\n",alloc,free);
      }
  }
  </code></pre>