1 2 Previous Next 16 Replies Latest reply: Oct 8, 2009 1:46 PM by jschellSomeoneStoleMyAlias RSS

    ResultSet and Statement objects - are they REALLY closed???

    807580
      Hello,

      I have a question with regards to ResultSet and Statement objects.

      After I'm done using these objects in a program, I close them with their close( ) methods, and do the same for the Connection object as well.

      But are the ResultSet and Statement objects really closing?

      After I used the ResultSet and Statement objects to make a request to the db, and after calling close( ) on each object, I did some out.print's with some 'if' statements... here are the results :
      rs :org.apache.tomcat.dbcp.dbcp.DelegatingResultSet@1b4c1d7
      stmt :com.mysql.jdbc.StatementImpl@2e323
      conn :null
      1) If the ResultSet and Statement objects are not returning null, are they still in memory?
      2) If they are in memory, would setting them to Null take care of that?

      I look forward to getting your expertise on this question.
        • 1. Re: ResultSet and Statement objects - are they REALLY closed???
          Darryl Burke
          But are the ResultSet and Statement objects really closing?
          To know that, sysout rs.isClosed() and stat.isClosed(). Not rs and stat.
          1) If the ResultSet and Statement objects are not returning null, are they still in memory?
          As long as they are referenced by any variable that is in scope, yes. After that, maybe.
          2) If they are in memory, would setting them to Null take care of that?
          Objects cannot be null, only references can be set to null. Setting the variable reference to null will make the object eligible for garbage collection, provided no other reference exists.

          But why does this bother you?

          db
          • 2. Re: ResultSet and Statement objects - are they REALLY closed???
            807580
            [http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html#close()]
            [http://java.sun.com/javase/6/docs/api/java/sql/Statement.html#close()]
            • 3. Re: ResultSet and Statement objects - are they REALLY closed???
              807580
              With other words, you expected that they become null after calling the close() .. ?

              How ever would you do that in Java? That an object reference becomes null after calling a method on it?
              • 4. Re: ResultSet and Statement objects - are they REALLY closed???
                807580
                By thinking that Java is somehow pass by reference? (jk)
                • 5. Re: ResultSet and Statement objects - are they REALLY closed???
                  807580
                  These database objects are associated with resources other than heap memory. A ResultSet, for example, is associated with a database cursor, which uses resources inside the database server.

                  The point of the close call is to release these resources, not the ResultSet object itself.

                  Once closed the object still exists until it becomes unreachable and is eligible for garbage collection in the usual way.
                  • 6. Re: ResultSet and Statement objects - are they REALLY closed???
                    807580
                    DarrylBurke wrote:
                    But are the ResultSet and Statement objects really closing?
                    To know that, sysout rs.isClosed() and stat.isClosed(). Not rs and stat.
                    1) If the ResultSet and Statement objects are not returning null, are they still in memory?
                    As long as they are referenced by any variable that is in scope, yes. After that, maybe.
                    2) If they are in memory, would setting them to Null take care of that?
                    Objects cannot be null, only references can be set to null. Setting the variable reference to null will make the object eligible for garbage collection, provided no other reference exists.

                    But why does this bother you?

                    db
                    It bothers me because I'm testing out my website and I've noticed two problems.

                    1) Sometimes when I access my site, I get a blank page with 'Done' in the status bar. When I hit Refresh, everything displays as it should. (The host provider says they are not experiencing this problem.)
                    2) I've been monitoring my resource usage, and each time I access a page (the pages are Java Server Pages), my memory usage increases....and continues to do so with each request.

                    If resources were being properly released, wouldn't memory usage fluctuate, and not just continue to increase whereas there should be increase AND decrease??

                    So in short, I'm looking for answers as to why this is happening. Its pretty obvious, based on my resource usage, that there is a memory issue here.

                    If you need to ask me any questions that will help pinpoint the problem, I'll do my best to answer them. Thank you for your help.
                    • 7. Re: ResultSet and Statement objects - are they REALLY closed???
                      796440
                      webnotick wrote:
                      2) I've been monitoring my resource usage, and each time I access a page (the pages are Java Server Pages), my memory usage increases....and continues to do so with each request.

                      If resources were being properly released, wouldn't memory usage fluctuate, and not just continue to increase whereas there should be increase AND decrease??
                      There are lots of ways to waste memory besides not closing sockets, streams, connections, etc. Those close calls work, so if you're calling them properly in finally blocks like you should be, these resources will not be leaking memory. You can easily test this by writing a tiny program that does nothing but open a socket or DB connection or whatever and then close it, in a tight loop.

                      It's perfectly acceptable for the VM's memory usage to grow even if all references are being properly released. In my experience, it's normal for the VM to NOT give memory back to the OS, even when GC does run. And GC may not be running, even though there is memory that could be freed, because it doesn't need to if you haven't yet hit the heap limit assigned to the VM at startup.
                      • 8. Re: ResultSet and Statement objects - are they REALLY closed???
                        796440
                        webnotick wrote:
                        Its pretty obvious, based on my resource usage, that there is a memory issue here.
                        Based on what you've said here, it is most definitely NOT obvious that you have a "memory issue."
                        • 9. Re: ResultSet and Statement objects - are they REALLY closed???
                          807580
                          webnotick wrote:
                          2) I've been monitoring my resource usage, and each time I access a page (the pages are Java Server Pages), my memory usage increases....and continues to do so with each request.

                          If resources were being properly released, wouldn't memory usage fluctuate, and not just continue to increase whereas there should be increase AND decrease??
                          If you monitor a graph of heap usage (as you can with jconsole) you'll typically see a "saw tooth" pattern. Heap usage rises steadily until the garbage collector cuts in, then it abruptly falls. If you aren't using much of the available heap the GC point may never be reached.
                          • 10. Re: ResultSet and Statement objects - are they REALLY closed???
                            807580
                            In my experience, it's normal for the VM to NOT give memory back to the OS
                            When I played with such tests (admittedly some time ago) I found that it was version-dependent (and did not appear to be platform-dependent) whether the JVM was willing to return memory back to the OS.
                            • 11. Re: ResultSet and Statement objects - are they REALLY closed???
                              JoachimSauer
                              BIJ001 wrote:
                              In my experience, it's normal for the VM to NOT give memory back to the OS
                              When I played with such tests (admittedly some time ago) I found that it was version-dependent (and did not appear to be platform-dependent) whether the JVM was willing to return memory back to the OS.
                              -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio can be used to [tweak this on the Sun JVM|http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp]. But I don't have any experience with those.
                              • 12. Re: ResultSet and Statement objects - are they REALLY closed???
                                807580
                                jverd wrote:
                                There are lots of ways to waste memory besides not closing sockets, streams, connections, etc. Those close calls work, so if you're calling them properly in finally blocks like you should be, these resources will not be leaking memory. You can easily test this by writing a tiny program that does nothing but open a socket or DB connection or whatever and then close it, in a tight loop.
                                Yes, I do call the close methods in the finally block. But I also noticed that I call them right before the finally, which is redundant code, and something I should probably get rid of.
                                       rs.close();
                                             stmt.close();             
                                             conn.close();             
                                                                  
                                            }//try
                                                
                                             catch (Exception e) { }
                                              
                                
                                             finally { 
                                            try { 
                                              if (rs!=null) rs.close();
                                                    }
                                                 catch(SQLException e){}                 
                                               try { 
                                              if (stmt!=null) stmt.close();
                                                   }
                                                 catch(SQLException e){}
                                                 
                                               try { 
                                              if (conn!=null) conn.close();
                                                   }
                                                 catch(SQLException e){}                  
                                                 
                                               }//finally
                                It's perfectly acceptable for the VM's memory usage to grow even if all references are being properly released. In my experience, it's normal for the VM to NOT give memory back to the OS, even when GC does run. And GC may not be running, even though there is memory that could be freed, because it doesn't need to if you haven't yet hit the heap limit assigned to the VM at startup.
                                I'm going to test this out right now and see if the memory is freed after it hits the limit. The JVM heap is currently set to 64 initial, and 128 maximum. I am permitted to set the maximum to 128.
                                I will post an update once I complete testing.
                                Thank you for the post.
                                • 13. Re: ResultSet and Statement objects - are they REALLY closed???
                                  796440
                                  webnotick wrote:
                                  But I also noticed that I call them right before the finally, which is redundant code,
                                  Yes.
                                  and something I should probably get rid of.
                                  Yes.

                                  The whole point of finally is to do cleanup work no matter how the try and catch blocks complete. Whether everything goes smoothly or there's an exception, break, continue, or return, you put your cleanup in one place--finally.
                                  • 14. Re: ResultSet and Statement objects - are they REALLY closed???
                                    807580
                                    jverd wrote:
                                    webnotick wrote:
                                    Its pretty obvious, based on my resource usage, that there is a memory issue here.
                                    Based on what you've said here, it is most definitely NOT obvious that you have a "memory issue."
                                    Your right, I checked the Tomcat log file, and from what I've read online, if there was an out of memory instance, there would've been a log recorded for that.

                                    I tested and monitored memory usage just now. I monitored it through the control panel of the server, and through SSH.
                                    It reached 201mb used, and 87mb free - out of 288mb. I really tried to get the site to exceed memory allowance, and it won't. At least not yet;-)
                                    1 2 Previous Next