Subject Possible memory leak in EncodingFactory ?
Author chiss14
Hello everyone,

I ran into a problem using JayBird v2.0.1 with the embedded version
of FireBird. It could very well be that I'm not using the right
technique to query the db, I'm still quite new with this.

Anyway, a part of my application was doing a lot of plain SQL select
and I was always running out of memory.

Using NetBeans's profiler, I found that there was close to 7000
instances of Encoding_cp1252 ... So I went to get the source code,
found the beta 2.1.0 and traced my problem to the EncodingFactory
class. It seems like its always returning new instances of
Encoding_xxxx (which I guess in normal circumstances are getting
cleaned up by the GC, but this wasn't happening in mine ;-) ).

So to make a long story short, I looked what the Encoding_xxx classes
were doing, assumed they only perform "translation" work, i.e. could be
reused easily. So I did the following quick hack :

private static HashMap encodingCache = new HashMap();

public static Encoding getEncoding(String encoding){

if (encoding == null) encoding = defaultEncoding;

if (!encodingCache.containsKey(encoding))
encodingCache.put(encoding,createEncoding(encoding));

return (Encoding)encodingCache.get(encoding);

//return createEncoding(encoding); }



...and this works like a charm! My (quite limited) tests looked ok, no
adverse effects were observed. My memory usage is down (like, waaaaay
down), which is all I wanted. Maybe this cache should go in a HashTable
instead of an HashMap in a multithreaded context, I just don't know
enough about JayBird's code.

Any thoughts ?

Francis



[Non-text portions of this message have been removed]