Subject Re[2]: [Firebird-Java] Possible memory leak in EncodingFactory ?
Author Alexey Panchenko
Roman Rokytskyy wrote:

>> Encoding result = (Encoding)translatorCache.get(encoding);
>> if (result == null) {
>> result = getEncodingInternal(encoding, charMapping);
>> translatorCache.put(encoding, charMapping);
>> }

Shouldn't it be "translatorCache.put(encoding, result);" ?
Or it should be cached at all - what is the role of charMapping
argument - should it be include in cache key ?

>> if (EncodingFactory.USE_ENCODING_CACHING) {
>> char[] bufferC = new char[in.length];
>> int length = decodeFromCharset(in, 0, in.length, bufferC);
>> return new String(bufferC, 0, length);

The reason of slowdown is double memory allocations above:
- first is "new char[]"
- second is during "new String" - the char[] is copied into the String

Without pooling the shared char[] is used and only new String is

Probably we can use the String constructor:

public String(byte bytes[], int offset, int length, String charsetName)

Though it can be slower than internal implementation.

Best regards,
Alexey mailto:alex+news@...