title: ArrayList Out of Memory Error
body: <p>I am trying to Retrieve 100,000 records from an SQL database but I am having an Out of Memory Error when using <code>ArrayList</code>. </p>
  
  <p>How can I optimize my code to achieve this? How can I apply a flyweight design pattern to my code?</p>
  
  <pre><code>import java.math.BigDecimal;
  import java.util.*;
  
  public class Foo {
      public List&lt;Vo&gt; Service(Vo vo) throws Exception {
          HashMap&lt;Integer, Object&gt; param = null;
          ArrayList&lt;HashMap&lt;String, Object&gt;&gt; getRows = null;
          HashMap&lt;String, Object&gt; row = null;
          Vo Vo = null;
  
          try {
              List&lt;Vo&gt; list = new ArrayList&lt;Vo&gt;();
  
              if (Vo != null) {
                  param = new HashMap();
  
                  if (Vo.getCode() != null) {
                      param.put(1, Vo.getCode());
                      param.put(2, Vo.getCode());
                  }
                  if (Vo.getCode() != null) {
                      param.put(3, Vo.getCode());
                      param.put(4, Vo.getCode());
                  }
  
                  getRows = (ArrayList) ImpDAO.RetrieveQuery(param);
  
                  if ((getRows != null) &amp;&amp; (getRows.size() &gt; 0)) {
                      for (Iterator iterator = getRows.iterator(); iterator.hasNext();) {
                          Vo = new Vo();
                          row = (HashMap) iterator.next();
  
                          if (row.get("CODE") != null)
                              Vo.setDept((BigDecimal) (row.get("CODE")));
                          if (row.get("SER") != null)
                              Vo.setServCode(row.get("SER").toString());
                          if (row.get("NAME") != null)
                              list.add(Vo);
                      }
                  }
              }
          } catch (Exception ae) {
              throw ae;
          }
  
          return list;
      }
  }
  </code></pre>