0

I am trying to release the space used by JDBC ResultSet by closing it and setting the value to null in the finally block. Ex:

try{
  //some query executed here and resultset used to get the values from the db
}catch(Exception exp){
 //log exception here
}finally{
 //close resultset
  if(rs != null){
  rs.close();
  rs = null;
 }
}

When I check for the number of open ResultSet in YourKit, even though all the resultsets are closed and set to null, instead of showing 0 resultset open YourKit is showing some non zero value for open resultsets. Can anyone please help me here to make the number of open resultset 0.

Thanks in advance. Tanmayee

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
tanmayees
  • 1
  • 2
  • IIRC, a connection could hold on to the resultsets it created for as long as it stays open. I've come across this behaviour whil researching a memory leak in a multithreaded application. See if the results are collected when closing the connection. – 0xCAFEBABE Nov 22 '13 at 07:11

4 Answers4

2

As a YourKit developer, I would suggest to reproduce the situation when ResultSets are in memory (when they shouldn't) and capture memory snapshot.

After that you will be able to find a paths from your ResultSets to garbage collector roots. It will be a reson why ResultSets are being leaked. Here is a link to help how to work with GC paths http://www.yourkit.com/docs/java/help/paths.jsp

1

Garbage collection in java is not same as C, where you deallocate memory and it will instantly release the memory, In Java its up to the JVM when to run the garbage collector, some of the possible conditions may be out of memory condition when the JVM calls the garbage collector, and one is when we call System.gc() method however calling this method doesn't results in calling the garbage collector. In above case you are making an object eligible for the garbage collection, you are not deallocating anything from the memory.

Bilbo Baggins
  • 2,899
  • 10
  • 52
  • 77
  • 1
    To clarify Aayush's statement… When you call `System.gc()`, you are making a *request* of the JVM to collect garbage, not a demand. The JVM may or may not collect garbage at the time of your request. So your ResultSet objects may remain in memory as candidates for garbage collection. See [doc](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#gc()). – Basil Bourque Nov 22 '13 at 07:07
  • Thank you Aayush and Basil for the quick reply. So are you saying that even though the the count of ResultSet showing in YourKit is not 0, it may not be a case of memory leak? – tanmayees Nov 22 '13 at 07:34
0

close may throw error which may cause open ResultSet (rare but possible scenario), ideally db resource cleaning should happen in try-catch:

finally {
if(rs != null) {
try{
 rs.close();
}catch(Exception e) {
 //handle, log ..
}
}

With this atleast you can browse logs for close failures and be in sync with YourKit!

harsh
  • 7,502
  • 3
  • 31
  • 32
  • Thank you harsh for the suggestion. I will add this in the code to avoid exception that may throw at the time of closing the ResultSet. – tanmayees Nov 22 '13 at 07:35
0

In finally block you can write like this then it will works because ,Here why we are using try-catch block means sometimes ResultSet object may available or not available.

finally{
try{
 if(rs!=null){
 rs.close();

   }}catch(Exception e){
e.printStackTrace();
}
 }