title: Store 2d matrix in CSV: java.lang.OutOfMemoryError
body: <p>I wrote the following function for calculating cost matrix and storing it in CSV file:</p>
  
  <pre><code>private static void calculateCostMatrix()
  {
      int len = _POIs.size();
      CostMatrix = new double[len][len];
      for (int i=0; i&lt;len; i++)
      {
          int ic = _POIs.get(i).getId();
          for (int j=i; j&lt;len; j++)
          {
              int jc = _POIs.get(j).getId();
              double dist = euclideandist(_POIs.get(i).getLat(),_POIs.get(i).getLon(),
                                      _POIs.get(j).getLat(),_POIs.get(j).getLon());
              CostMatrix[ic][jc] = dist;
              CostMatrix[jc][ic] = dist;
          }           
      }
  
      // Save in CSV
      try
      {
          String NEW_LINE = System.getProperty("line.separator");
          File file = new File("CostMatrix.csv");
          FileWriter fw = new FileWriter(file.getAbsoluteFile()); 
          BufferedWriter bw = new BufferedWriter(fw);
          StringBuilder sb = new StringBuilder();;
          for (double[] row : CostMatrix) 
          {
              for (double d : row)
              {
                   sb.append(d);
                   sb.append(",");
              }
              sb.append(NEW_LINE);
          }
          bw.write(sb.toString());
          bw.close();
          }
  
          bw.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>However, obviously, due to large number of elements (200,000 x 200,000) I get the message <code>java.lang.OutOfMemoryError: Java heap space</code>.</p>
  
  <p>So, instead of pre-allocating a space for CostMAtrix, I now want to directly calculate and store each cell in CSV file.
  How can I do this in the proper way?</p>