Need help in effective memory management for below scenario. I am fetching data from two different databases and comparing data in Java.(currently testing on single database with two queries).
As 9.8 million records needs to be compared, I am copying 50k records each time and loading into ArrayList and comparing using Binarysearch. Though I am clearing (assigning to null and running gc) the arraylist after every iteration, I am getting Heap space error(Assigned 1GB RAM) after comparing 2.5 million records.
Where is the memory leakage in my query?
Query1= select empno,ename from table1 order by empno;
Query2= select empno,ename from table2 order by empno;
ResultSet rs1 = st1.executeQuery(query1);
ResultSet rs2 = st2.executeQuery(query2);
for (;;) {
ArrayList<String> al = new ArrayList<String>();
ArrayList<String> al1 = new ArrayList<String>();
if (totalRecords1 == Ubound)
break;
Lbound = Ubound + 1;
Ubound = min(Ubound + 50000, totalRecords1);
System.out.println("Lbound : " + Lbound);
System.out.println("Ubound : " + Ubound);
for (int i = Lbound; i <= Ubound; i++) {
recordConcat1 = ""; recordConcat2 = "";
String recordConcat1 = "", recordConcat2 = "";
rs1.next();
rs2.next();
recordConcat1 = recordConcat1 + rs1.getString(z) + " ǀ ";
recordConcat2 = recordConcat2 + rs2.getString(z) + " ǀ ";
al.add(recordConcat1);
al1.add(recordConcat2);
} /* End of First Lap */
System.out.println("End of Lap : "+lap++);
int index =0;
for(int like=0;like<al.size();like++) {
if(Collections.binarySearch(al1,al.get(like))>=0)
continue;
else {
System.out.println("Not matched : "+ al.get(like));
break;
}
}
al =null;
al1=null;
System.gc();
} /* End of Infinite Loop */