Received: from www.vgkk.com ([64.56.164.18]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5K3ffa27219 for ; Tue, 19 Jun 2001 23:41:42 -0400 (EDT) (envelope-from rmager@vgkk.com) Received: from [61.202.206.130] (helo=rmagervm) by www.vgkk.com with smtp (Exim 3.22 #7) id 15CYs4-0001xR-00 for pgsql-admin@postgresql.org; Wed, 20 Jun 2001 12:41:36 +0900 From: "Rainer Mager" To: "PostgreSQL Admin" Subject: RE: High memory usage [PATCH] Date: Wed, 20 Jun 2001 12:40:22 +0900 Message-ID: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_0027_01C0F986.2BE1B500" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 In-Reply-To: Importance: Normal X-Archive-Number: 200106/112 X-Sequence-Number: 1669 This is a multi-part message in MIME format. ------=_NextPart_000_0027_01C0F986.2BE1B500 Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: 7bit Attached is my patch to the official 7.1.2 PreparedStatement.java class. This seems to work quite well for me in a test case. To try to reproduce the seen problem I will need to test all night. I'll report tomorrow. BTW, this is my first attempt at making a unified diff so I might have done something wrong. If this diff doesn't apply please tell me. --Rainer > -----Original Message----- > From: pgsql-admin-owner@postgresql.org > [mailto:pgsql-admin-owner@postgresql.org]On Behalf Of Rainer Mager > Sent: Wednesday, June 20, 2001 9:08 AM > To: Tom Lane > Cc: PostgreSQL Admin > Subject: RE: [ADMIN] High memory usage > > > I'll work on a patch but if someone has already done this I would be > grateful. ------=_NextPart_000_0027_01C0F986.2BE1B500 Content-Type: application/octet-stream; name="PreparedStatement.java.diff" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="PreparedStatement.java.diff" --- PreparedStatement.java Sat Feb 17 01:45:00 2001 +++ PreparedStatement.java.new Wed Jun 20 12:34:57 2001 @@ -39,10 +39,12 @@ // Some performance caches private StringBuffer sbuf =3D new StringBuffer(); =20 - // We use ThreadLocal for SimpleDateFormat's because they are not = that - // thread safe, so each calling thread has its own object. - private ThreadLocal tl_df =3D new ThreadLocal(); // setDate() Si= mpleDateFormat - private ThreadLocal tl_tsdf =3D new ThreadLocal(); // setTimestamp= () SimpleDateFormat + // Because SimpleDateFormat is not thread safe we create one for each + // PreparedStatemnt here AND synchronize on it for each usage. + // We can NOT use ThreadLocal because they are not freed until the thread + // completes. This would be a memory leak for long running threads. + private SimpleDateFormat df =3D null; + private SimpleDateFormat tsdf =3D null; =20 /** * Constructor for the PreparedStatement class. @@ -90,9 +92,6 @@ * New in 7.1 - overides Statement.close() to dispose of a few loc= al objects */ public void close() throws SQLException { - // free the ThreadLocal caches - tl_df.set(null); - super.close(); } =20 @@ -332,14 +331,18 @@ */ public void setDate(int parameterIndex, java.sql.Date x) throws SQLExcept= ion { - SimpleDateFormat df =3D (SimpleDateFormat) tl_df.get(); - if(df=3D=3Dnull) { + // The df DateFormat is initialized here to delay creation until needed. + if( df =3D=3D null ) { + synchronized( this ) { + if( df =3D=3D null ) { df =3D new SimpleDateFormat("''yyyy-MM-dd''"); - tl_df.set(df); + } + } } =20 - set(parameterIndex, df.format(x)); - + // We must synchronize here because SimpleDateFormat is not thread safe. + synchronized( df ) { + set( parameterIndex, df.format(x) ); // The above is how the date should be handled. // // However, in JDK's prior to 1.1.6 (confirmed with the @@ -351,6 +354,7 @@ // //set(parameterIndex, df.format(new java.util.Date(x.getTime()+86400000= ))); } + } =20 /** * Set a parameter to a java.sql.Time value. The driver converts @@ -375,17 +379,22 @@ */ public void setTimestamp(int parameterIndex, Timestamp x) throws SQLExcep= tion { - SimpleDateFormat df =3D (SimpleDateFormat) tl_tsdf.get(); - if(df=3D=3Dnull) { - df =3D new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - tl_tsdf.set(df); + // The tsdf DateFormat is initialized here to delay creation until neede= d. + if( tsdf =3D=3D null ) { + synchronized( this ) { + if( tsdf =3D=3D null ) { + tsdf =3D new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + tsdf.setTimeZone(TimeZone.getTimeZone("GMT")); + } + } } - df.setTimeZone(TimeZone.getTimeZone("GMT")); =20 + // We must synchronize here because SimpleDateFormat is not thread safe. + synchronized( tsdf ) { // Use the shared StringBuffer synchronized(sbuf) { sbuf.setLength(0); - sbuf.append("'").append(df.format(x)).append('.').append(x.get= Nanos()/10000000).append("+00'"); + sbuf.append("'").append(tsdf.format(x)).append('.').append(x.getNanos(= )/10000000).append("+00'"); set(parameterIndex, sbuf.toString()); } =20 @@ -393,6 +402,7 @@ // to be identical. Pays to read the docs ;-) //set(parameterIndex,"'"+x.toString()+"'"); } + } =20 /** * When a very large ASCII value is input to a LONGVARCHAR parameter, ------=_NextPart_000_0027_01C0F986.2BE1B500--