I have an application that iterates over an amount of resultSets and queries additional information for every row from a different table.
The rough structure is like this:
public void main(String[] args) {
ResultSet result = database.connection.createStatement()
.executeQuery("SELECT * FROM entities");
ArrayList<Entity> entities = new ArrayList<Entity>();
while (result.next() {
Entity entity = EntityFactory.createById(result.getInt("id"));
entities.add(entity);
}
}
// EntityFactory
public static Entity createById(int id) {
StringBuilder sql = new StringBuilder("SELECT * FROM sampling_data WHERE id = ")
.append(id);
ResultSet result = database.connection.createStatement()
.executeQuery(sql.toString());
result.first();
EntityData data = new EntityData(25);
for (int sample = 1; sample <= 25; sample++) {
String sample_R = new StringBuilder("sample_")
.append(sample).append("_R").toString();
String sample_G = new StringBuilder("sample_")
.append(sample).append("_G").toString();
String sample_B = new StringBuilder("sample_")
.append(sample).append("_B").toString();
int r = resultSet.getInt(sample_R);
int g = resultSet.getInt(sample_G);
int b = resultSet.getInt(sample_B);
data.add(r, g, b);
}
return new Entity(data);
}
Which results in a OutOfMemoryException.
How can I make the loop (or the whole methods) more memory efficient?
The largest problem was (I think) that createById did not close result set and statement.
Further it makes sense to not have nx1 queries.
And