title: reading small CSV File in java resulting in out of memory error
body: <p>I have the following two implementation of reading csv files, the csv files in question are not that large(5 megabytes).  </p>
  
  <p>The first implementation is using openCSV, the second one is using stringTokenizer.  </p>
  
  <p>The first one resulted in out of memory error even when I raised the java max heap memory to 1G(Xmx), although the StringTokenizer implementation is not robust, but I have no choice as I need to read the csv file into memory.</p>
  
  <p>I don't understand why the openCSV version would consume so much memory given the small size of the csv file(it has 200k rows, but only about 5m file size).  what is openCSV reader doing that would require so much memory?  The StringTokenizer version breezes through it in no time.</p>
  
  <p>here's the error thrown by the openCSV implementation:</p>
  
  <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
      at java.util.Arrays.copyOfRange(Arrays.java:3209)
      at java.lang.String.&lt;init&gt;(String.java:215)
      at java.lang.StringBuilder.toString(StringBuilder.java:430)
      at au.com.bytecode.opencsv.CSVParser.parseLine(Unknown Source)
      at au.com.bytecode.opencsv.CSVParser.parseLineMulti(Unknown Source)
      at au.com.bytecode.opencsv.CSVReader.readNext(Unknown Source)
  </code></pre>
  
  <hr>
  
  <pre><code>private List&lt;String[]&gt; parseCSV(File f) {
      List&lt;String[]&gt;res=new Vector&lt;String[]&gt;();
      CSVReader reader=null;
      try{
          reader = new CSVReader(new BufferedReader(new FileReader(f)));
          String [] nextLine;
          while ((nextLine = reader.readNext()) != null) {
              for(int i=0;i&lt;nextLine.length;i++)if(nextLine[i]!=null)nextLine[i]=nextLine[i].trim();
              res.add(nextLine);
          }
  
      }catch(IOException exp){
          exp.printStackTrace();
      }finally{
          if(reader!=null)try {
              reader.close();
          } catch (IOException ex) {
              Logger.getLogger(DataStream2.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
      return res;
  
  }
  
   private List&lt;String[]&gt; parseCSV(File f) {
      List&lt;String[]&gt;res=new Vector&lt;String[]&gt;();
      BufferedReader br=null;
      try{
          br = new BufferedReader(new FileReader(f));
          String line =null;
          while((line=br.readLine())!=null){
              StringTokenizer st=new StringTokenizer(",");
              String[]cur=new String[st.countTokens()];
              for(int i=0;i&lt;cur.length;i++){
                  cur[i]=st.nextToken().trim();
              }
              res.add(cur);
          }
      }catch(IOException exp){
          exp.printStackTrace();
       }
      finally{
          if(br!=null)try {
              br.close();
          } catch (IOException ex) {
              Logger.getLogger(DataStream2.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
      return res;
  }
  </code></pre>