db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dyre.Tjeldv...@Sun.COM
Subject Re: Garbage Collector and memory
Date Tue, 25 Oct 2005 08:45:11 GMT
Juan Ignacio Villa <ji_villa@yahoo.es> writes:

> Hello.
>
> My java application uses memory more and more until it
> crash. It is strange because not use new variables nor
> I do not create them dynamically. I have an for with
> which I make select SQL by means of inserted dynamic
> values. The peculiar thing is that in each return of
> for for, the program uses memory more and more, but I
> do not create new variables, but reusing same
> variables. I proved to garbage it with runtime.gc but
> there was no difference. Dont free it. 
>
> I can do it of other way, but I have discovered that
> gc does not work to me in the application. It does not
> release memory until the application finalizes, and
> cannot be in a server application. 
> It is necessary to make some extra task to release
> statements, resultsets or connections? 
> The used memory comes from another part? 
>
> derbydev.pdf tells that the memory to gc is closed
> normally. Also I have seen that driver it maintains a
> list of resultset not closed and does not release the
> memory.
>
> My code is:
>
> Class.forName"org.apache...");
> java.sql.Connection DbConexion =
> DriverManager.getConnection("jdbc:derby:" + addpath
> +"/prueba....");
>
> String sql ="";
> Statement Consulta =null;
> ResultSet Rs = null;
>
> for (int i=1; i<400; i++) {
>   sql="SELECT * FROM tipo2 WHERE " + consul[i];
    // This creates a new dynamic object

>   Consulta =
> DBConexion.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
  // ... as does this
>   Rs = Consulta.executeQuery(sql);
  // .. and this

>   Rs.close();
>   Rs = null;
>   Consulta.close();
>   Consulta = null;
> }
>
> try{
>   Rs.close();
>   Consulta.close();
>   Rs = null;
>   Consulta = null;
> }catch(...)
>

Try using prepared statements instead. That is something like (untested):

PreparedStatement Consulta = DBConnexion.prepareStatement("SELECT *
FROM tipo2 WHERE <someColumn> <op> ?"); 
// Replace <someColumn> and <op> as appropriate

for (int i=1; i<400; i++) {
   Consulta.set<Type>(1, consul[i]); // Replace <Type> with the type of consul[i]
   Rs = Consulta.executeQuery();
   // Do something with Rs ??
   Rs.close();
}


-- 
dt

However, experience shows that for many people and many applications a
dose of paranoia is reasonable - Bjarne Stroustrup


Mime
View raw message