users@glassfish.java.net

Re: OutOfMemoryError: heap space (SJSAS PE 8.2 u4)

From: <glassfish_at_javadesktop.org>
Date: Fri, 19 Dec 2008 04:21:23 PST

We have found a possible cause of memory leak by analyzing of heap memory dump using the HAT.

There was a many of unclosed statement instances, because we use the next code in some places of our applications:

PreparedStatement stmt = null;
stmt = connection.prepareStatement(...);
rs = stmt.executeQuery();
//do something
stmt = connection.prepareStatement(...);
rs = stmt.executeQuery();
//do something
rs.close();
stmt.close();

Here created a two statement instances, but closed the second only...
We have changed it to the next code:

PreparedStatement stmt = null;
stmt = connection.prepareStatement(...);
rs = stmt.executeQuery();
//do something
rs.close();
stmt.close();
stmt = connection.prepareStatement(...);
rs = stmt.executeQuery();
//do something
rs.close();
stmt.close();

And I will see the result soon.

PS: In the API specification the following is written about Statement.close() method:
--
Note: A Statement object is automatically closed when it is garbage collected. When a Statement object is closed, its current ResultSet object, if one exists, is also closed.
--
I think it's not garbage collected because pooled connection have pool of PreparedStatement objects and holds reference on each of they.
[Message sent by forum member 'mikamj' (mikamj)]
http://forums.java.net/jive/thread.jspa?messageID=322618