Re: Memory leak ?

From: Lew <noone(at)lewscanon(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Memory leak ?
Date: 2011-04-06 22:40:36
Message-ID: iniq52$ovi$1@news.albasani.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Guillaume Cottenceau wrote:
> Pierre Le Mouëllic writes:
>
>> With this code :
>>
>> public class TestJDBC {
>
> [...]
>
>> 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();
>
> e.g. you explicitely close only the last resultset of the three
> you produced (assuming no exception)

So? Why is that an issue?

<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."

>> In eclipse Helios profiler, Jdbc3ResultSet live instances increase (and never
>> decrease). Same thing with
>
> Garbage collection is not predictable, closing resultsets [sic] (and
> statements) is strongly adviced if you want to release JDBC
> resources in a timely manner.

No.

Closing a 'ResultSet' is actually not advised. Well, sometimes, but generally
you should close its 'Statement'. In the OP's case he did the right thing by
closing just the last 'ResultSet', since he wanted his 'Statement' to stay alive.

>
> I have never used this profiler but it would be interesting to
> know if any references are keeping these objects alive (then we
> could begin thinking of a leak), or if they are only not yet
> collected by the GC because it decided not to do it yet for any
> reason.

I tend not to conflate the issues of memory resources and external resources
like database connections.

> Interestingly, the diff between:
>
> http://download.oracle.com/javase/1.4.2/docs/api/java/sql/Statement.html#close()
>
> and:
>
> http://download.oracle.com/javase/6/docs/api/java/sql/Statement.html#close()
>
> shows they've removed "Statement object is automatically closed
> when it is garbage collected." no idea why though. probably a
> more seasoned PG-JDBC dev/user would have?

One should never have depended exclusively on the GC to close resources in any
event.

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

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Craig Ringer 2011-04-07 02:54:42 Re: Recommended resource type and settings for J2EE/Glassfish connection pool
Previous Message Lew 2011-04-06 22:34:50 Re: Memory leak ?