pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Vitalii Tymchyshyn <tivv00@gmail.com>
To: Imran <imranbohoran@gmail.com>
Cc: Kevin Grittner <Kevin.Grittner@wicourts.gov>
Cc: Dave Cramer <pg@fastcrypt.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: non-trivial finalize() on AbstractJdbc2Statement
Date: Wed, 15 Feb 2012 13:30:15 +0200
Message-ID: <4F3B9747.2010804@gmail.com> (raw)
In-Reply-To: <CAG6o=AH=NrCk7SZQ+zaKSehjajnoECn6YGs-YDW7S_SCv3FogQ@mail.gmail.com>
References: <CAG6o=AHE4yfi5E6Ec2FBE=0o3f=QPbN54Fr_HZAysc+m7nJ-Pw@mail.gmail.com>
	<CADK3HH+rKyg+ss71B0J9AaaKjQduHUj6MYrVaB_qmDaWmPsNew@mail.gmail.com>
	<CAG6o=AFRjZAEVi_+LSgYSszE669gEjcNBYn6qcHNgZeGMWmHxA@mail.gmail.com>
	<4F390FF2.2040500@gmail.com>
	<CAG6o=AGivBfOaytb+0PA2Wp-PMpiYZWyxUNxOj51tFr35AZXcQ@mail.gmail.com>
	<CABWW-d3tz=Nhk3PtfkaQ1MEcU9XTDFDmLzmV3xgdQqfijcEk+g@mail.gmail.com>
	<4F38EAC5020000250004545C@gw.wicourts.gov>
	<CAG6o=AH=NrCk7SZQ+zaKSehjajnoECn6YGs-YDW7S_SCv3FogQ@mail.gmail.com>

Hello.

The finalizer thread is not a low priority thread in my JVM (openjdk) 
and as soon as Statement.close does nothing (and it does nothing if 
statement is closed), I don't see how can it be a problem to free 
memory. Actually statement creation is much more heavy thing than "if 
(flag) return". It's perfectly valid to close native resource (and as 
far as I know statement allocates server-side native resources) in 
finalizer. Another implementation can be to create a ReferenceQueue and 
a bunch of References that should be checked now and then during 
connection calls, but this introduces additional processing that is not 
needed most time. You can't drop this code altogether as while it's good 
practice to close statements in client code, you can't guarantee it's 
done everywhere. Actually it's common practice in java to clean-up 
external resources in finalize, see for example FileOutputStream.
As of your problem, I don't thing statement finalize method is the 
problem cause. I can see two causes:
1) The code bug mentioned in this thread
2) Some another class with long finalize method. This is the case that 
can lead to confusion. Say, you have an object created once in 5 minutes 
that has 3 minutes finalize. It won't hurt by itself as it's enough time 
to be finalized. But as soon as you have additional object with 
finalized method defined (say, Statement), even empty, all this objects 
can't be freed during this 3 minutes run and you will get OOM. And you 
will rarely blame your custom object. The best check that can be done to 
show real reason is to check Finalizer object stack with jstack. I did 
often see cases when custom finalize method is fast, but is synchronized 
by some heavy-used  resource.

Best regards, Vitalii Tymchyshyn


15.02.12 13:11, Imran ???????(??):
> Hi Guys
>
> I dont think I'm referring to the speed or workings of the finalize() 
> method available on AbstractJdbc2Statement. As said earlier I agree 
> that it should return quickly if statements are closed by clients and 
> also having safeguards around it looks nice. But my point is that by 
> having a non-trivial finalize() an extra JdbcxStatement object is been 
> created and referenced through a Finalizer, which is in a reference 
> queue that would be picked up by the Finalizer thread. And a GC 
> wouldn't reclaim the memory until the finalizer thread has done its 
> work. As we have many queries executing, the main thread keeps filling 
> this queue at a much faster rate than the finalizer thread (which is a 
> low priority thread by default) would get to it. Under heavy load this 
> paves the way for possible OOM issues as we've noticed. Also just to 
> check this, we've patched up the driver we have without the finalize() 
> method and we've seen much better GC and have not experienced OOM on 
> similar load that resulted in OOMs.
>
> The general feeling as I've learned over the years is that finalize() 
> should be used cautiously. So I was wondering if its really required 
> for the AbstractJdbc2Statement to have a finalize() that only closes 
> the statement, which one would expect the client to do anyway.
>
> Cheers
> -- Imran
>
> On Mon, Feb 13, 2012 at 4:49 PM, Kevin Grittner 
> <Kevin.Grittner@wicourts.gov <mailto:Kevin.Grittner@wicourts.gov>> wrote:
>
>     ******* ********<tivv00@gmail.com <mailto:tivv00@gmail.com>> wrote:
>     > Looking at the code... Can it be because
>     > org.postgresql.jdbc2.AbstractJdbc2Statement#isClosed is not
>     > volatile? There is no synchronization and finalizer thread may
>     > simply not see statement was just closed by another thread.
>
>     That sounds likely enough to me.  I don't know whether declaring the
>     flag volatile would be enough, but it needs either that or access
>     only through synchronized blocks.
>
>     In addition, I would recommend something like the attached to make
>     the code more bullet-proof.
>
>     -Kevin
>
>

view thread (16+ messages)  latest in thread

Message-ID: <4F3B9747.2010804@gmail.com>
Permalink:  ../4F3B9747.2010804@gmail.com/
Also on:    postgresql.org/message-id/4F3B9747.2010804@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: tivv00@gmail.com, imranbohoran@gmail.com, Kevin.Grittner@wicourts.gov, pg@fastcrypt.com
  Subject: Re: non-trivial finalize() on AbstractJdbc2Statement
  In-Reply-To: <4F3B9747.2010804@gmail.com>
  References: <CAG6o=AHE4yfi5E6Ec2FBE=0o3f=QPbN54Fr_HZAysc+m7nJ-Pw@mail.gmail.com> <CADK3HH+rKyg+ss71B0J9AaaKjQduHUj6MYrVaB_qmDaWmPsNew@mail.gmail.com> <CAG6o=AFRjZAEVi_+LSgYSszE669gEjcNBYn6qcHNgZeGMWmHxA@mail.gmail.com> <4F390FF2.2040500@gmail.com> <CAG6o=AGivBfOaytb+0PA2Wp-PMpiYZWyxUNxOj51tFr35AZXcQ@mail.gmail.com> <CABWW-d3tz=Nhk3PtfkaQ1MEcU9XTDFDmLzmV3xgdQqfijcEk+g@mail.gmail.com> <4F38EAC5020000250004545C@gw.wicourts.gov> <CAG6o=AH=NrCk7SZQ+zaKSehjajnoECn6YGs-YDW7S_SCv3FogQ@mail.gmail.com> <4F3B9747.2010804@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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