Soheil Soheil - 1 year ago 94
Java Question

mysql Memory (RAM) usage increases while using ResultSet?

I am using MySQL and Java to SELECT about 50000 records.
The strange thing is that when I use ResultSet and next() method to read the data, I see that the RAM usage of my java application increases during the fetching. it begins with 255 MB and increases up to 379 MB!
the code I am using is here:

try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/#mysql50#crawler - used in report?" + "user=root&password=&useUnicode=true&characterEncoding=UTF-8");
Statement st = conn.createStatement();
ResultSet rsDBReader = st.executeQuery("SELECT Id, Content FROM DocsArchive");
while ( {
int docId = rsDBReader.getInt(1);
String content = rsDBReader.getString(2);
. . .
} catch (Exception e) {
System.out.println("Exception in reading data: " + e);

I am sure that the memory usage is for ResultSet, not other parts of the program.
In this program I don't need to update records, so I want to remove every record after finishing the work.
My guess is that the records which have been read, will not be removed and the program doesn't free their memory. so I have used some tricks to avoid this, such as using following code:

Statement st = conn.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);


but they didn't change any thing. :(

So I need some method that removes (releases) memory of rows that have been read.

Another interesting point is that even after finishing the function and closing the ResultSet, Statement and Connection, and going to other part of the program, still the program memory usage doesn't decrease!

Answer Source

I'd suggest limiting the amount of rows you retrieve in your query. 50000 is a lot, so why not have a loop that fetches, let's say, 1000 rows every time?

You can achieve this using the limit statement, as described here. It's always best to be pragmatic about amount of data you're processing. Your current select might return 50000 rows today, but what if it grows to one million tomorrow? Your application will choke. So, do your processing step by step.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download