X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BF54B3A3D35 for ; Tue, 2 Nov 2004 14:40:09 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 66117-05 for ; Tue, 2 Nov 2004 14:40:00 +0000 (GMT) Received: from mx1.redhat.com (mx1.redhat.com [66.187.233.31]) by svr1.postgresql.org (Postfix) with ESMTP id CD24B3A3CFF for ; Tue, 2 Nov 2004 14:40:02 +0000 (GMT) Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.12.11/8.12.11) with ESMTP id iA2Ee1vu031906; Tue, 2 Nov 2004 09:40:01 -0500 Received: from mail.boston.redhat.com (mail.boston.redhat.com [172.16.76.12]) by int-mx1.corp.redhat.com (8.11.6/8.11.6) with ESMTP id iA2Ee1r08287; Tue, 2 Nov 2004 09:40:01 -0500 Received: from 192.168.1.2 (vpn83-142.boston.redhat.com [172.16.83.142]) by mail.boston.redhat.com (8.12.8/8.12.8) with ESMTP id iA2EdwFE024656; Tue, 2 Nov 2004 09:39:59 -0500 From: Vadim Nasardinov Organization: Red Hat / Westford To: Sebastiaan van Erk , pgsql-jdbc@postgresql.org Subject: Re: ps.setCharacterStream() and memory usage Date: Tue, 2 Nov 2004 09:46:09 -0500 User-Agent: KMail/1.5.4 References: <20041029115651.GE4199@sebster.com> In-Reply-To: <20041029115651.GE4199@sebster.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200411020946.09434@vadim.nasardinov> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/17 X-Sequence-Number: 11439 On Friday 29 October 2004 07:56, Sebastiaan van Erk wrote: > 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. [...] > [...] 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). I assume you're talking about org/postgresql/jdbc2/AbstractJdbc2Statement.java which has this piece of code (slightly reformatted to fit on a 80-column page): public void setCharacterStream(int i, Reader x, int length) throws SQLException { checkClosed(); if (length < 0) throw new PSQLException(GT.tr("Invalid stream length {0}.", new Integer(length))); if (connection.haveMinimumCompatibleVersion("7.2")) { char[] l_chars = new char[length]; int l_charsRead = 0; try { while(true) { int n = x.read(l_chars, l_charsRead, length - l_charsRead); if (n == -1) break; l_charsRead += n; if (l_charsRead == length) break; } } catch (IOException l_ioe) { throw new PSQLException(GT.tr("Provided Reader failed."), PSQLState.UNEXPECTED_ERROR, l_ioe); } setString(i, new String(l_chars, 0, l_charsRead)); } else { } } 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(GT.tr("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: http://www.google.com/search?q=Heinz+Kabutz+StringBuffer+Issue+068&btnI