Обсуждение: Memory leak ?

Поиск
Список
Период
Сортировка

Memory leak ?

От
Pierre Le Mouëllic
Дата:
<font face="Xerox Sans">With this code :<br /></font> <pre>public class TestJDBC {
   private static Timer timer;
   private static Connection connDB;   private static PreparedStatement sQLStatement;
   public static void main(String[] args) throws SQLException, ClassNotFoundException {
       connexion("jdbc:postgresql://praslin.qual.dc1:5432/G01DPM", "postgres8", "password");              timer = new
Timer();      timer.schedule(new local_task(), 1000L, 1000);       }
 
   static class local_task extends TimerTask {
       public void run() {           ResultSet rs=null;           try {               sQLStatement.setInt(1, 2602);
         rs = sQLStatement.executeQuery();               sQLStatement.setInt(1, 2604);               rs =
sQLStatement.executeQuery();              sQLStatement.setInt(1, 2605);               rs = sQLStatement.executeQuery();
         } catch (SQLException e) {           }           finally{
 
               try {                   if(rs!=null)                       rs.close();               } catch
(SQLExceptione) {               }               rollBack();           }       }   }
 
   public static void connexion(String chemin, String user, String password) throws SQLException,
ClassNotFoundException  {       Class.forName("org.postgresql.Driver");       String url =
chemin+"?user="+user+"&password="+password;
       //    Connexion       connDB = DriverManager.getConnection(url);       connDB.setAutoCommit(false);
initPreparedStatement();  }
 
   private static void initPreparedStatement() throws SQLException   {       sQLStatement =
connDB.prepareStatement("selectf_transite(?,'FPL','225',9,'test','','')");   }
 
   public static void rollBack()   {       try       {           connDB.rollback();       }       catch(SQLException
ex)      {       }   }
 
}</pre> In eclipse Helios profiler, Jdbc3ResultSet live instances increase (and never decrease). Same thing with
org.postgresql.core.v3.QueryExecutorImpl$ErrorTrackingResultHandlerand org.postgresql.core.v3.QueryExecutorImpl$1
classes.<br/><br /> f_transite stored procedure make some select, insert and update. If you need more explanation, i
cangive it to you.<br /><br /> Java 6<br /> postgresql-9.0-801.jdbc3.jar<br /> PostgreSQL 8.2.5 on
powerpc-apple-darwin8.8.0,compiled by GCC powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build
5363)<br/><br /> Is it a memory leak ? Or my java code is wrong ?<br /><br /><div class="moz-signature"><font
color="#000080"face="Arial" size="2"><b>Pierre LE MOUËLLIC<br /></b></font><font color="#000080" face="Arial"
size="1"><br/></font> <a href="mailto:pierre.lemouellic@xgs-france.com" moz-do-not-send="true"><font color="blue"
size="1"><u>pierre.lemouellic@xgs-france.com</u></font></a><br/><a href="mailto:pierre.lemouellic@xgs-france.com"
moz-do-not-send="true"><fontcolor="blue" size="1"><u></u></font></a></div> 

Re: Memory leak ?

От
Dave Cramer
Дата:
The garbage collector should be collecting those, so I would guess the
driver is holding a reference internally to the result set.

Dave Cramer

dave.cramer(at)credativ(dot)ca
http://www.credativ.ca




2011/4/6 Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>:
> With this code :
>
> public class TestJDBC {
>
>     private static Timer timer;
>
>     private static Connection connDB;
>     private static PreparedStatement sQLStatement;
>
>     public static void main(String[] args) throws SQLException,
> ClassNotFoundException {
>
>         connexion("jdbc:postgresql://praslin.qual.dc1:5432/G01DPM",
> "postgres8", "password");
>
>         timer = new Timer();
>         timer.schedule(new local_task(), 1000L, 1000);
>     }
>
>     static class local_task extends TimerTask {
>
>         public void run() {
>             ResultSet rs=null;
>             try {
>                 sQLStatement.setInt(1, 2602);
>                 rs = sQLStatement.executeQuery();
>                 sQLStatement.setInt(1, 2604);
>                 rs = sQLStatement.executeQuery();
>                 sQLStatement.setInt(1, 2605);
>                 rs = sQLStatement.executeQuery();
>             } catch (SQLException e) {
>             }
>             finally{
>
>                 try {
>                     if(rs!=null)
>                         rs.close();
>                 } catch (SQLException e) {
>                 }
>                 rollBack();
>             }
>         }
>     }
>
>     public static void connexion(String chemin, String user, String
> password) throws SQLException, ClassNotFoundException
>     {
>         Class.forName("org.postgresql.Driver");
>         String url = chemin+"?user="+user+"&password="+password;
>
>         //    Connexion
>         connDB = DriverManager.getConnection(url);
>         connDB.setAutoCommit(false);
>         initPreparedStatement();
>     }
>
>     private static void initPreparedStatement() throws SQLException
>     {
>         sQLStatement = connDB.prepareStatement("select
> f_transite(?,'FPL','225',9,'test','','')");
>     }
>
>     public static void rollBack()
>     {
>         try
>         {
>             connDB.rollback();
>         }
>         catch(SQLException ex)
>         {
>         }
>     }
> }
>
> In eclipse Helios profiler, Jdbc3ResultSet live instances increase (and
> never decrease). Same thing with
> org.postgresql.core.v3.QueryExecutorImpl$ErrorTrackingResultHandler and
> org.postgresql.core.v3.QueryExecutorImpl$1 classes.
>
> f_transite stored procedure make some select, insert and update. If you need
> more explanation, i can give it to you.
>
> Java 6
> postgresql-9.0-801.jdbc3.jar
> PostgreSQL 8.2.5 on powerpc-apple-darwin8.8.0, compiled by GCC
> powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build
> 5363)
>
> Is it a memory leak ? Or my java code is wrong ?
>
> Pierre LE MOUËLLIC
>
> pierre.lemouellic@xgs-france.com
>

Re: Memory leak ?

От
Mikko Tiihonen
Дата:
On 06/04/11 13:11, Pierre Le Mouëllic wrote:
> With this code :
>
> public class TestJDBC {
>
>      private static Timer timer;
>
>      private static Connection connDB;
>      private static PreparedStatement sQLStatement;
>
>      public static void main(String[] args) throws SQLException, ClassNotFoundException {
>
>          connexion("jdbc:postgresql://praslin.qual.dc1:5432/G01DPM","postgres8","password");
>
>          timer = new Timer();
>          timer.schedule(new local_task(), 1000L, 1000);
>      }
>
>      static class local_task extends TimerTask {
>
>          public void run() {
>              ResultSet rs=null;
>              try {
>                  sQLStatement.setInt(1, 2602);
>                  rs = sQLStatement.executeQuery();

Missing
rs.close();

>                  sQLStatement.setInt(1, 2604);
>                  rs = sQLStatement.executeQuery();

Missing
rs.close();

>                  sQLStatement.setInt(1, 2605);
>                  rs = sQLStatement.executeQuery();
>              } catch (SQLException e) {
>              }
>              finally{
>
>                  try {
>                      if(rs!=null)
>                          rs.close();
>                  } catch (SQLException e) {
>                  }
 >
>                  rollBack();
>              }
>          }
>      }
>
>      public static void connexion(String chemin, String user, String password) throws SQLException,
ClassNotFoundException
>      {
>          Class.forName("org.postgresql.Driver");
>          String url = chemin+"?user="+user+"&password="+password;
>
>          //    Connexion
>          connDB = DriverManager.getConnection(url);
>          connDB.setAutoCommit(false);
>          initPreparedStatement();
>      }
>
>      private static void initPreparedStatement() throws SQLException
>      {
>          sQLStatement = connDB.prepareStatement("select f_transite(?,'FPL','225',9,'test','','')");
>      }
>
>      public static void rollBack()
>      {
 >
>          try
>          {
>              connDB.rollback();
>          }
>          catch(SQLException ex)
>          {
>          }
>      }
> }
>
> In eclipse Helios profiler, Jdbc3ResultSet live instances increase (and never decrease). Same thing with
> org.postgresql.core.v3.QueryExecutorImpl$ErrorTrackingResultHandler and org.postgresql.core.v3.QueryExecutorImpl$1
classes.
>
> f_transite stored procedure make some select, insert and update. If you need more explanation, i can give it to you.
>
> Java 6
> postgresql-9.0-801.jdbc3.jar
> PostgreSQL 8.2.5 on powerpc-apple-darwin8.8.0, compiled by GCC powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple
Computer,Inc. build 5363) 
>
> Is it a memory leak ? Or my java code is wrong ?
>
> *Pierre LE MOUËLLIC
> *
> _pierre.lemouellic@xgs-france.com_ <mailto:pierre.lemouellic@xgs-france.com>
> __ <mailto:pierre.lemouellic@xgs-france.com>


Re: Memory leak ?

От
Pierre Le Mouëllic
Дата:
Same problem with adding rs.close() :<br /><br /> sQLStatement.setInt(1, 2602);<br /> rs =
sQLStatement.executeQuery();<br/> rs.close();<br /><br /> sQLStatement.setInt(1, 2604);<br /> rs =
sQLStatement.executeQuery();<br/> rs.close();<br /><br /> sQLStatement.setInt(1, 2605);<br /> rs =
sQLStatement.executeQuery();<br/> rs.close();<br /><br /><div class="moz-signature"><font color="#000080" face="Arial"
size="2"><b>PierreLE MOUËLLIC<br /><br /></b></font><a href="mailto:pierre.lemouellic@xgs-france.com"
moz-do-not-send="true"><fontcolor="blue" size="1"><u></u></font></a></div><br /> Le 06/04/2011 12:30, Mikko Tiihonen a
écrit :<blockquote cite="mid:4D9C40BA.3080606@nitorcreations.com" type="cite">On 06/04/11 13:11, Pierre Le Mouëllic
wrote:<br /><blockquote type="cite">With this code : <br /><br /> public class TestJDBC { <br /><br />      private
staticTimer timer; <br /><br />      private static Connection connDB; <br />      private static PreparedStatement
sQLStatement;<br /><br />      public static void main(String[] args) throws SQLException, ClassNotFoundException { <br
/><br/>          connexion("jdbc:postgresql://praslin.qual.dc1:5432/G01DPM","postgres8","password");<br /><br />
        timer = new Timer(); <br />          timer.schedule(new local_task(), 1000L, 1000); <br />      } <br /><br />
    static class local_task extends TimerTask { <br /><br />          public void run() { <br />              ResultSet
rs=null;<br />              try { <br />                  sQLStatement.setInt(1, 2602); <br />                  rs =
sQLStatement.executeQuery();<br /></blockquote><br /> Missing <br /> rs.close(); <br /><br /><blockquote
type="cite">                sQLStatement.setInt(1, 2604); <br />                  rs = sQLStatement.executeQuery(); <br
/></blockquote><br/> Missing <br /> rs.close(); <br /><br /><blockquote type="cite">                
sQLStatement.setInt(1,2605); <br />                  rs = sQLStatement.executeQuery(); <br />              } catch
(SQLExceptione) { <br />              } <br />              finally{ <br /><br />                  try { <br />
                    if(rs!=null) <br />                          rs.close(); <br />                  } catch
(SQLExceptione) { <br />                  } <br /></blockquote> > <br /><blockquote type="cite">                
rollBack();<br />              } <br />          } <br />      } <br /><br />      public static void connexion(String
chemin,String user, String password) throws SQLException, ClassNotFoundException <br />      { <br />         
Class.forName("org.postgresql.Driver");<br />          String url = chemin+"?user="+user+"&password="+password; <br
/><br/>          //    Connexion <br />          connDB = DriverManager.getConnection(url); <br />         
connDB.setAutoCommit(false);<br />          initPreparedStatement(); <br />      } <br /><br />      private static
voidinitPreparedStatement() throws SQLException <br />      { <br />          sQLStatement =
connDB.prepareStatement("selectf_transite(?,'FPL','225',9,'test','','')"); <br />      } <br /><br />      public
staticvoid rollBack() <br />      { <br /></blockquote> > <br /><blockquote type="cite">         try <br />         
{<br />              connDB.rollback(); <br />          } <br />          catch(SQLException ex) <br />          { <br
/>         } <br />      } <br /> } <br /><br /> In eclipse Helios profiler, Jdbc3ResultSet live instances increase
(andnever decrease). Same thing with <br /> org.postgresql.core.v3.QueryExecutorImpl$ErrorTrackingResultHandler and
org.postgresql.core.v3.QueryExecutorImpl$1classes. <br /><br /> f_transite stored procedure make some select, insert
andupdate. If you need more explanation, i can give it to you. <br /><br /> Java 6 <br /> postgresql-9.0-801.jdbc3.jar
<br/> PostgreSQL 8.2.5 on powerpc-apple-darwin8.8.0, compiled by GCC powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple
Computer,Inc. build 5363) <br /><br /> Is it a memory leak ? Or my java code is wrong ? <br /><br /> *Pierre LE
MOUËLLIC<br /> * <br /> _pierre.lemouellic@xgs-france.com_ <a class="moz-txt-link-rfc2396E"
href="mailto:pierre.lemouellic@xgs-france.com"><mailto:pierre.lemouellic@xgs-france.com></a><br/> __ <a
class="moz-txt-link-rfc2396E"
href="mailto:pierre.lemouellic@xgs-france.com"><mailto:pierre.lemouellic@xgs-france.com></a><br/></blockquote><br
/><br/></blockquote> 

Re: Memory leak ?

От
Lew
Дата:
Mikko Tiihonen wrote:
> Pierre Le Mouëllic wrote:
>> With this code :
>>
>> public class TestJDBC {
>>
>>      private static Timer timer;
>>
>>      private static Connection connDB;
>>      private static PreparedStatement sQLStatement;
>>
>>      public static void main(String[] args) throws SQLException, ClassNotFoundException {
>>
>>          connexion("jdbc:postgresql://praslin.qual.dc1:5432/G01DPM","postgres8","password");
>>
>>          timer = new Timer();
>>          timer.schedule(new local_task(), 1000L, 1000);
>>      }
>>
>>      static class local_task extends TimerTask {
>>
>>          public void run() {
>>              ResultSet rs=null;
>>              try {
>>                  sQLStatement.setInt(1, 2602);
>>                  rs = sQLStatement.executeQuery();

> Missing
> rs.close();

Not needed.

 From <http://download.oracle.com/javase/6/docs/api/java/sql/ResultSet.html>:

"A ResultSet object is automatically closed when the Statement object that
generated it is closed, re-executed, or used to retrieve the next result from
a sequence of multiple results."


>>                  sQLStatement.setInt(1, 2604);
>>                  rs = sQLStatement.executeQuery();

> Missing
> rs.close();

Nope.

In fact, it's rather an antipattern to call 'ResultSet#close()' explicitly.

>>                  sQLStatement.setInt(1, 2605);
>>                  rs = sQLStatement.executeQuery();
>>              } catch (SQLException e) {
>>              }
>>              finally{
>>
>>                  try {
>>                      if(rs!=null)
>>                          rs.close();

What's bizarre here is that we aren't closing the statement.

That's weird.

>>                  } catch (SQLException e) {
>>                  }
>>
>>                  rollBack();
>>              }
>>          }
>>      }

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg