From: Sebastiaan van Erk <sebster@sebster.com>
To: pgsql-jdbc@postgresql.org
Subject: ps.setCharacterStream() and memory usage
Date: Fri, 29 Oct 2004 13:56:52 +0200
Message-ID: <20041029115651.GE4199@sebster.com> (raw)
Hi,
Coming back to another problem I have with the following insert statement:
(Postgres 7.4, driver build 215, jdbc 3)
ps.setCharacterStream(1, reader, (int) messageFile.length());
ps.executeUpdate();
The reason I do this (using a character stream to load a text file to a
TEXT field) is that I wish to avoid loading the file into memory completely.
I already noticed that the postgres driver does not stream the data to
the backend (I don't know if postgres actually supports this).
But what is worse, is that the file actually gets copied THREE times
into memory, causing my Java file to already get a java.lang.OutOfMemory
error with an insert of a 10M text file (Java allocates 20M for this,
since it has 2 byte chars, and the driver makes (at least) 3 copies
(referenced at the same time, not allowing one to be GC'ed), making 60M).
First of all, in setCharacterStream() it loads the file into a char array.
Then this is cloned into a String and passed to setString. (Here one can
already cause the char[] to go out of scope, at least allowing it to be
cleaned up). Secondly, setString causes another clone in escapeString().
(Ideally, one could read the stream into a StringBuffer big enough to allow
it to be escaped, and thereby only load the file into memory once instead
of three times). Finally, the driver (possibly) keeps large objects in
memory too long: for example, in setString() right after the bind, one can
already do a "sbuf.setLength(0)". This way, one does not have to wait
for setString (or another setter) to be called before the sbuf variable
is cleared.
Greetings,
Sebastiaan van Erk
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: sebster@sebster.com
Subject: Re: ps.setCharacterStream() and memory usage
In-Reply-To: <20041029115651.GE4199@sebster.com>
References: <20041029115651.GE4199@sebster.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