Subject RE: [Firebird-Java] Re: Should (Could) FBStatements be held in WeakHashMap
Author Robert DiFalco
Well, not to pick too much of a nit, but there is actually a noticeable
overhead in using the "finalize" method. For very large applications it
could have a perceivable impact on garbage collection. When any object
has a finalize method those objects cannot move directly to the
deallocated state. In other words, the GC has to do more work when a
finalize method is present even if the method body does nothing.

If you are going to use WeakReferences, it is probably a better idea to
create your own hash-map so that you can give each WeakReference your
own singleton ReferenceQueue for statements (since you'd want the same
reference queue for all connections).

Something like:

private void collectReferences()
{
Reference ref;
while ( ( ref = m_referenceQueue.poll() ) != null )
{
final Statement statement = (Statement)ref.get();
try
{
statement.close();
}
....
}
}

But of course, then you need a thread to process this. But maybe JayBird
already has a background thread for this sort of processing. Another
problem with the stock WeakHashMap is that every time it resizes, it
calls WeakHashMap.expungeStaleEntries. This blocks while it goes through
the queue and processes discarded elements, removing them from the map.
Actually, I think ever call has this logic since it is embedded in
WeakHashmap.getTable.

So basically, I have to block and go through this logic every time I
create or prepare a new statement or every time I close the statement or
connection explicitly (which I'm supposed to do anyway).

At least if you ditch WeakHashMap and use a single reference-queue in a
background thread for all statements across all connection, well then I
won't have to block on preparing statements to look for discarded
references. Of course, I'm never going to NOT close statements that I
open (unless I want them open), so all of it seems kinda like a drag.
Maybe not a huge drag, but still more stuff to protect people from
themselves, stuff that ONLY benefits those who are using the API's
incorrectly in the first place.

R.

-----Original Message-----
From: Mark O'Donohue [mailto:mark.odonohue@...]
Sent: Saturday, June 14, 2003 9:22 AM
To: Firebird-Java@yahoogroups.com
Subject: Re: [Firebird-Java] Re: Should (Could) FBStatements be held in
WeakHashMap


Robert DiFalco wrote:
> I just wanted to state my concern for adding memory and processor
> overhead to benefit those whose code is in error. After all, closing a
> statement is a requirement, not just "a good thing to do". I'm not
crazy
> about having to pay with the maintenance of Weak References, Reference
> Queues, and casts for those whose code is in error.
>

The overhead is very trivial and the usage is not in a heavily
time/memory critial region of operation.

The main extra resourse used is a weak reference object per Statement
object. So it's not like it was every string operation for instance.


> If this is an issue (and I'm not sure why it should be), I would
prefer
> to have a DEBUG mode that tracks statements and their resource
> consumption. By turning this flag on I could tell that I'm am not
> properly closing resources without imposing a hit on those who are
using
> resources properly.
>

Sure it's worth checking, but the implemntation is fairly simple, and
fairly close to as cost efficient as the original, checking a debug flag

would likely consume more cpu time than was added to the routine.


Cheers

Mark