I am trying to Retrieve 100,000 records from an SQL database but I am having an Out of Memory Error when using ArrayList
.
How can I optimize my code to achieve this? How can I apply a flyweight design pattern to my code?
import java.math.BigDecimal;
import java.util.*;
public class Foo {
public List<Vo> Service(Vo vo) throws Exception {
HashMap<Integer, Object> param = null;
ArrayList<HashMap<String, Object>> getRows = null;
HashMap<String, Object> row = null;
Vo Vo = null;
try {
List<Vo> list = new ArrayList<Vo>();
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) && (getRows.size() > 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;
}
}