Skip site navigation (1) Skip section navigation (2)

Re: ps.setCharacterStream() and memory usage

From: Sebastiaan van Erk <sebster(at)sebster(dot)com>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: ps.setCharacterStream() and memory usage
Date: 2004-11-02 15:08:49
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-jdbc
Vadim Nasardinov wrote:

> I assume you're talking about
> org/postgresql/jdbc2/ which has this piece
> of code (slightly reformatted to fit on a 80-column page):

> This does appear to be optimized for reading smallish chunks of text.
> If the character source behind the Reader is sufficiently large, I
> believe we'd be better off building up a StringBuffer rather than a
> character array.  Something like this:
>     if (connection.haveMinimumCompatibleVersion("7.2")) {
>         StringBuffer sb = new StringBuffer(length);
>         BufferedReader br = new BufferedReader(x);
>         try {
>             while(true) {
>                 String chunk = br.readLine();
>                 if (chunk == null) { break; }
>                 else {sb.append(chunk); }
>             }
>         } catch (IOException l_ioe) {
>             throw new PSQLException("Provided Reader failed."),
>                                     PSQLState.UNEXPECTED_ERROR, l_ioe);
>         }
>         setString(i, sb.toString());
>     }
> The reason this is better is because
>   (a) the String(char[],int,int) constructor always defensively copies
>       the passed in character array.  So, as you point out, if you
>       pass in a 10-million-character array, a new 10-million character
>       array will be allocated.
>   (b) in contrast, if you construct a String from a StringBuffer, they
>       can share the underlying character array:

This is indeed part of the code I was talking about. And indeed a
StringBuffer is already a definate improvement.

However, the setString() method will also cause the data to be duplicated
when escapeString() is called. A version of escapeString() which works
on a StringBuffer would be another improvement in my opinion, saving
(if the allocated StringBuffer is made a bit larger to allow for the
escaping), in most cases another allocation of the whole string.

Sebastiaan van Erk

In response to


pgsql-jdbc by date

Next:From: Alan StangeDate: 2004-11-02 16:04:53
Subject: executeBatch() issue with new driver?
Previous:From: Vadim NasardinovDate: 2004-11-02 14:46:09
Subject: Re: ps.setCharacterStream() and memory usage

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group