• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory problem with objects of JDBC

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I have created a connection object saying
Connection con = DriverManager.getConnection()
and then executed a query by creating a statement object.
Statement stmt = con.createStatement();
i have also closed the connection and the stmt object saying
stmt.close();
con.close();

but i would like to know will there be any memory problem or will the objects con and stmt occupy space in memory if we dont say the following statements after stmt.close() and con.close()
stmt=null;
con=null;
i guess including the above statements will help the GC to collect the objects when it is run. if we dont have the above two statements then the memory occupied by the above two objects may or may not be released.
can anyone help me in this regard
thanks
shekar.
 
Joe Ess
Bartender
Posts: 9340
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chandra Bairi:

stmt=null;
con=null;
i guess including the above statements will help the GC to collect the objects when it is run. if we dont have the above two statements then the memory occupied by the above two objects may or may not be released.

It depends on the scope of stmt and con. If they are declared in a method, they will go out of scope when the method ends and explicity setting them to null is not necessary for them to be GC'd. If they are instance variables, they will last as long as the instance that declares them exists or until the references are nulled. If they are static class variables they will be around for the life of the program or until their references are set to null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic