I have a program that issues a query that returns roughly 150k datapoints in a result set for each of 30 queries. I'm trying to clean up the memory from processing which pushes the JVM up to 130MB RAM usage. There are two threads, one that issues the database query and gets the results and another that does the processing of the results.
The first thread takes the result set and deep copies it into a List>, which is put onto a shared Queue between the threads. The second thread takes off of this and then sets it to null. (Currently for memory usage testing). The first thread surrounds the DB call with a finally that sets the statement and result set to null. Commenting out the queue and 2nd thread, memory is 70MB usage. But when I put it back in, memory stays at 130MB usage. Any ideas why it doesn't go back down to 70?
Statement stmt = null;
ResultSet rs = null;
try
{
if (conn.isValid(DB_TIMEOUT))
{
String query = msg.getQuery();
Object request = msg.getRequest();
// errorLog.debug("Query Issued to Trend Database: " + query);
stmt = conn.createStatement();
if (!query.isEmpty())
{
stmt.execute(query);
rs = stmt.getResultSet();
}
TrendApp.resultSetProcQueue.put(new ResultData((jTrendDataRequest) request, resultSetToArrayList(rs)));
} catch (SQLException e)
{
errorLog.error("Failed to issue database command: " + e);
} finally
{
if (stmt != null)
{
try
{
stmt.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs != null)
{
try
{
rs.close();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
On the processing side:
while (true)
{
ResultData result = null;
try
{
try
{
result = (ResultData) TrendApp.resultSetProcQueue.take();
} catch (InterruptedException e)
{
errorLog.error("Unable to fetch message from Data Adapter processing queue.");
}
} finally
{
if (result != null)
{
result.setData(null);
result = null;
}
}
}
Here is the resultSet to ArrayList conversion
public List<HashMap<String, Object>> resultSetToArrayList(ResultSet rs) throws SQLException
{
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
while (rs.next())
{
HashMap<String, Object> row = new HashMap<String, Object>(columns);
for (int i = 1; i <= columns; ++i)
{
row.put(md.getColumnName(i), rs.getObject(i));
}
list.add(row);
}
return list;
}