Re: java.lang.OutOfMemoryError using MySQL
Posted by: Mark Matthews
Date: March 24, 2005 07:50AM

hendrik.ladner wrote:
> Hi,
>
> my application visualizes data contained in a
> MySQL database.
>
> To avoid unnecessary database queries if the
> visualization has to be repainted
> a global ResultSet variable exists within my class
> that holds the actual data.
> If a repaint is invoked I go through the
> ResultSet, paint the data and set the ResultSet
> cursor back to the beginning. So I need a
> ResultSet like
>
> Statement st =
> con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV
> E, ResultSet.CONCUR_READ_ONLY);
> rs = st.executeQuery(query);
>
> If the user scrolls or zooms the visualization I
> have to update the ResultSet because different
> data has to be displayed.
>
> After some updates of the ResultSet I get a
> java.lang.OutOfMemoryError. I read in many forums
> that it is possible to tell the MySQL driver with
>
> Statement st = con.createStatement();
> st.setFetchSize(Integer.MIN_VALUE);
>
> not to store the whole ResultSet at the client
> side, but this does not work with scrollable
> ResultSets. So this is no option for me.
>
> My problem is, why does the memory usage of the
> JVM increases until the memory error occurs?
> Code example:
>
> public class MyClass {
>
> private ResultSet rs;
>
> private void repaint() {
> if(updateNecessary) {
> rs = getDataFromDB(...);
> }
> while(rs.next) {
> // draw the data
> }
> rs.beforeFirst();
> }
> }
>
> I inserted debug messages that showed the memory
> usage of the JVM. Nearly each update of the
> ResultSet increased the used memory. It looks like
> the garbage collector is not able or to slow to
> free the no longer used resources of the
> ResultSet.
>
> I tried to close the ResultSet before the update,
> but this resulted in a NullPointerException.
>
> Any ideas?
>
> Many thanks and best regards
> Hendrik


Hendrik,

You need to call .close() on result sets to release the memory they use, otherwise because of the way you have your application constructed (the global reference), the memory can 'leak' until you get out-of-memory errors because the garbage collector can never 'see' that the result set can be collected.

I'd like to see the full stack trace of your NullPointerException, because I have a feeling (we don't have your entire code here, so it's only a guess) that somewhere you refer to a null reference in your code after closing the result set. If our result set implementation itself is throwing a NullPointerException it's a bug, but we'd still need to see the full stack trace so we know what's going on.

-Mark

Mark Matthews
Consulting Member Technical Staff - MySQL Enterprise Tools
Oracle
http://www.mysql.com/products/enterprise/monitor.html

Options: ReplyQuote


Subject
Written By
Posted
Re: java.lang.OutOfMemoryError using MySQL
March 24, 2005 07:50AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.