pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedMemory leak ?
10+ messages / 6 participants
[nested] [flat]
* Memory leak ?
@ 2011-04-06 10:11 Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
0 siblings, 2 replies; 10+ messages in thread
From: Pierre Le Mouëllic @ 2011-04-06 10:11 UTC (permalink / raw)
To: pgsql-jdbc
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
</head>
<body bgcolor="#ffffff" text="#000000">
<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 (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)
{
}
}
}</pre>
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.<br>
<br>
f_transite stored procedure make some select, insert and update. 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>
<div class="moz-signature"><font face="Arial" color="#000080"
size="2"><b>Pierre LE MOUËLLIC<br>
</b></font><font face="Arial" color="#000080" size="1"><br>
</font>
<a moz-do-not-send="true"
href="mailto:pierre.lemouellic@xgs-france.com"><font
color="blue" size="1"><u>pierre.lemouellic@xgs-france.com</u></font></a><br>
<a moz-do-not-send="true"
href="mailto:pierre.lemouellic@xgs-france.com"><font
color="blue" size="1"><u></u></font></a>
</div>
</body>
</html>
^ permalink raw reply [nested|flat] 10+ messages in thread
* Memory leak ?
@ 2011-04-06 10:18 Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
0 siblings, 2 replies; 10+ messages in thread
From: Pierre Le Mouëllic @ 2011-04-06 10:18 UTC (permalink / raw)
To: pgsql-jdbc
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
</head>
<body bgcolor="#ffffff" text="#000000">
<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 (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)
{
}
}
}</pre>
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.<br>
<br>
f_transite stored procedure make some select, insert and update. 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>
<div class="moz-signature"><font face="Arial" color="#000080"
size="2"><b>Pierre LE MOUËLLIC<br>
</b></font><font face="Arial" color="#000080" size="1"><br>
</font> <a moz-do-not-send="true"
href="mailto:pierre.lemouellic@xgs-france.com"><font
color="blue" size="1"><u>pierre.lemouellic@xgs-france.com</u></font></a><br>
</div>
</body>
</html>
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Memory leak ?
@ 2011-04-06 10:30 Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
parent: Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
1 sibling, 2 replies; 10+ messages in thread
From: Mikko Tiihonen @ 2011-04-06 10:30 UTC (permalink / raw)
To: pgsql-jdbc
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>
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Memory leak ?
@ 2011-04-06 10:47 Dave Cramer <pg@fastcrypt.com>
parent: Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
1 sibling, 0 replies; 10+ messages in thread
From: Dave Cramer @ 2011-04-06 10:47 UTC (permalink / raw)
To: Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>; +Cc: pgsql-jdbc
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
>
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Memory leak ?
@ 2011-04-06 10:50 Oliver Jowett <oliver@opencloud.com>
parent: Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
1 sibling, 0 replies; 10+ messages in thread
From: Oliver Jowett @ 2011-04-06 10:50 UTC (permalink / raw)
To: Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>; +Cc: pgsql-jdbc
2011/4/6 Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>:
> 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.
When profiling, has a full GC & weak reference collection happened
before you count "live instance"?
Probably those "live" instances are only weakly reachable and will be
cleaned up later.
(The driver uses weak references + a reference queue that is polled
before each query execution to clean up server-side allocated
resources corresponding to JDBC statements / resultsets that were not
properly closed, just discarded)
If that's not the problem, I'd need to know the path from a GC root to
the live instances.
Oliver
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Memory leak ?
@ 2011-04-06 10:52 Guillaume Cottenceau <gc@mnc.ch>
parent: Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
1 sibling, 1 reply; 10+ messages in thread
From: Guillaume Cottenceau @ 2011-04-06 10:52 UTC (permalink / raw)
To: Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>; +Cc: pgsql-jdbc
Pierre Le Mouëllic <pierre.lemouellic 'at' xgs-france.com> 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)
> In eclipse Helios profiler, Jdbc3ResultSet live instances increase (and never
> decrease). Same thing with
Garbage collection is not predictable, closing resultsets (and
statements) is strongly adviced if you want to release JDBC
resources in a timely manner.
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.
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?
--
Guillaume Cottenceau
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Memory leak ?
@ 2011-04-06 10:59 Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
parent: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
1 sibling, 0 replies; 10+ messages in thread
From: Pierre Le Mouëllic @ 2011-04-06 10:59 UTC (permalink / raw)
To: pgsql-jdbc
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
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 face="Arial" color="#000080"
size="2"><b>Pierre LE MOUËLLIC<br>
<br>
</b></font><a moz-do-not-send="true"
href="mailto:pierre.lemouellic@xgs-france.com"><font
color="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 static Timer 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 (SQLException e) {
<br>
}
<br>
finally{
<br>
<br>
try {
<br>
if(rs!=null)
<br>
rs.close();
<br>
} catch (SQLException e) {
<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 void initPreparedStatement() throws
SQLException
<br>
{
<br>
sQLStatement = connDB.prepareStatement("select
f_transite(?,'FPL','225',9,'test','','')");
<br>
}
<br>
<br>
public static void 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 (and never decrease). Same thing with
<br>
org.postgresql.core.v3.QueryExecutorImpl$ErrorTrackingResultHandler
and 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 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>
</body>
</html>
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Memory leak ?
@ 2011-04-06 22:34 Lew <noone@lewscanon.com>
parent: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
1 sibling, 0 replies; 10+ messages in thread
From: Lew @ 2011-04-06 22:34 UTC (permalink / raw)
To: pgsql-jdbc
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
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Memory leak ?
@ 2011-04-06 22:40 Lew <noone@lewscanon.com>
parent: Guillaume Cottenceau <gc@mnc.ch>
0 siblings, 1 reply; 10+ messages in thread
From: Lew @ 2011-04-06 22:40 UTC (permalink / raw)
To: 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
^ permalink raw reply [nested|flat] 10+ messages in thread
* Re: Memory leak ?
@ 2011-04-07 07:05 Guillaume Cottenceau <gc@mnc.ch>
parent: Lew <noone@lewscanon.com>
0 siblings, 0 replies; 10+ messages in thread
From: Guillaume Cottenceau @ 2011-04-07 07:05 UTC (permalink / raw)
To: Lew <noone@lewscanon.com>; +Cc: pgsql-jdbc
Lew <noone 'at' lewscanon.com> writes:
> 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?
Did I say it is 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."
Yes, it is not an issue.
>>> 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'.
I'm no expert on that, so it's one more reason why I'm not going
to lose too much time on that by answering once to illustrate my
claims, and then let it go if that creates more fuss. I hope I
can learn from this actually so that's not a call from you to not
answer that message if you see wrong claims.
I'm stating closing (of resultsets and statements) is adviced
from memories of messages on this list and other lists, and this
seems to be supported by the documentation over there:
http://download.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#close()
In particular:
"Releases this ResultSet object's database and JDBC resources
immediately instead of waiting for this to happen when it is
automatically closed."
I don't see why it would be "actually not adviced" as you say. So
there is an API, whose name is "close" which is similar to other
names used in java API to immediately release external resources
instead of waiting for the GC to do so, and whose documentation
says the same thing, but it would be adviced to not use it? Seems
odd.
> In the OP's case he did the right thing by closing just the
> last 'ResultSet', since he wanted his 'Statement' to stay
> alive.
Closing the two first resultsets would not allow this statement
to stay alive? That doesn't seem supported by the documentation
of the close method.
--
Guillaume Cottenceau
^ permalink raw reply [nested|flat] 10+ messages in thread
end of thread, other threads:[~2011-04-07 07:05 UTC | newest]
Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-04-06 10:11 Memory leak ? Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
2011-04-06 10:30 ` Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
2011-04-06 10:59 ` Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
2011-04-06 22:34 ` Lew <noone@lewscanon.com>
2011-04-06 10:47 ` Dave Cramer <pg@fastcrypt.com>
2011-04-06 10:18 Memory leak ? Pierre Le Mouëllic <pierre.lemouellic@xgs-france.com>
2011-04-06 10:50 ` Oliver Jowett <oliver@opencloud.com>
2011-04-06 10:52 ` Guillaume Cottenceau <gc@mnc.ch>
2011-04-06 22:40 ` Lew <noone@lewscanon.com>
2011-04-07 07:05 ` Guillaume Cottenceau <gc@mnc.ch>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox