From 8b4e1e1cf8f5d753ed901406f73d67b21557fddb Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Wed, 6 Jan 2021 10:57:57 -0800 Subject: [PATCH 1/2] 8258956: Memory Leak in StringCoding on ThreadLocal resultCached StringCoding.Result --- .../share/classes/java/lang/StringCoding.java | 27 ++++--- .../lang/StringCoding/ResultCachedGCTest.java | 79 +++++++++++++++++++ 2 files changed, 97 insertions(+), 9 deletions(-) create mode 100644 test/jdk/java/lang/StringCoding/ResultCachedGCTest.java diff --git a/src/java.base/share/classes/java/lang/StringCoding.java b/src/java.base/share/classes/java/lang/StringCoding.java index 8c0911b169acf..5193e7b3c8e89 100644 --- a/src/java.base/share/classes/java/lang/StringCoding.java +++ b/src/java.base/share/classes/java/lang/StringCoding.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -523,16 +523,25 @@ static byte[] encode(byte coder, byte[] val) { private static native void err(String msg); /* The cached Result for each thread */ - private static final ThreadLocal + private static final ThreadLocal> resultCached = new ThreadLocal<>() { - protected StringCoding.Result initialValue() { - return new StringCoding.Result(); + protected SoftReference initialValue() { + return new SoftReference<>(new Result()); }}; + private static Result resultCached() { + SoftReference sr = resultCached.get(); + Result r; + if (sr == null || (r = sr.get()) == null) { + r = new Result(); + resultCached.set(new SoftReference<>(r)); + } + return r; + } ////////////////////////// ascii ////////////////////////////// private static Result decodeASCII(byte[] ba, int off, int len) { - Result result = resultCached.get(); + Result result = resultCached(); if (COMPACT_STRINGS && !hasNegatives(ba, off, len)) { return result.with(Arrays.copyOfRange(ba, off, off + len), LATIN1); @@ -582,7 +591,7 @@ private static byte[] encodeASCII(byte coder, byte[] val) { ////////////////////////// latin1/8859_1 /////////////////////////// private static Result decodeLatin1(byte[] ba, int off, int len) { - Result result = resultCached.get(); + Result result = resultCached(); if (COMPACT_STRINGS) { return result.with(Arrays.copyOfRange(ba, off, off + len), LATIN1); } else { @@ -720,13 +729,13 @@ private static void throwUnmappable(byte[] val) { private static Result decodeUTF8(byte[] src, int sp, int len, boolean doReplace) { // ascii-bais, which has a relative impact to the non-ascii-only bytes if (COMPACT_STRINGS && !hasNegatives(src, sp, len)) - return resultCached.get().with(Arrays.copyOfRange(src, sp, sp + len), + return resultCached().with(Arrays.copyOfRange(src, sp, sp + len), LATIN1); return decodeUTF8_0(src, sp, len, doReplace); } private static Result decodeUTF8_0(byte[] src, int sp, int len, boolean doReplace) { - Result ret = resultCached.get(); + Result ret = resultCached(); int sl = sp + len; int dp = 0; @@ -1057,7 +1066,7 @@ static String newStringNoRepl1(byte[] src, Charset cs) { } catch (CharacterCodingException x) { throw new IllegalArgumentException(x); // todo } - Result ret = resultCached.get().with(ca, 0, cb.position()); + Result ret = resultCached().with(ca, 0, cb.position()); return new String(ret.value, ret.coder); } diff --git a/test/jdk/java/lang/StringCoding/ResultCachedGCTest.java b/test/jdk/java/lang/StringCoding/ResultCachedGCTest.java new file mode 100644 index 0000000000000..f3ab26faddd90 --- /dev/null +++ b/test/jdk/java/lang/StringCoding/ResultCachedGCTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* @test + * @bug 8258956 + * @summary Check if resultCached thread local is properly gc'ed. + * @run main/othervm -Xmx256m -XX:SoftRefLRUPolicyMSPerMB=0 -verbose:gc ResultCachedGCTest + */ + +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.locks.ReentrantLock; + +public class ResultCachedGCTest { + private static final int NUM_THREADS = 100; + private static final int BA_LENGTH = 100_000_000; + private static final byte[] ba = new byte[BA_LENGTH]; + private static final List workers = new ArrayList<>((int)(NUM_THREADS / 0.75f) + 1); + private static final ReentrantLock rl = new ReentrantLock(); + private static final CountDownLatch doneSignal = new CountDownLatch(NUM_THREADS); + + public static void main(String[] args) throws Exception { + for (int i = 0; i < NUM_THREADS; i++) { + var w = new Worker(); + workers.add(w); + var t = new Thread(w); + t.setDaemon(true); + t.start(); + } + + doneSignal.await(); + } + + static class Worker implements Runnable { + @Override + public void run() { + rl.lock(); // one 'new String()' at a time. + try { + System.out.println(Thread.currentThread()); + new String(ba, 0, BA_LENGTH, StandardCharsets.UTF_8); + } catch (OutOfMemoryError oome) { + throw new RuntimeException("StringCoding.resultCached was not properly GC'ed.", oome); + } finally { + rl.unlock(); + doneSignal.countDown(); + } + // keep it alive + while (true) { + try { + new CountDownLatch(1).await(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } +} From dee95d3231ae1fcd84ef92b810c77a18d00594cf Mon Sep 17 00:00:00 2001 From: Naoto Sato Date: Wed, 13 Jan 2021 10:04:04 -0800 Subject: [PATCH 2/2] Removed the test case. --- .../lang/StringCoding/ResultCachedGCTest.java | 79 ------------------- 1 file changed, 79 deletions(-) delete mode 100644 test/jdk/java/lang/StringCoding/ResultCachedGCTest.java diff --git a/test/jdk/java/lang/StringCoding/ResultCachedGCTest.java b/test/jdk/java/lang/StringCoding/ResultCachedGCTest.java deleted file mode 100644 index f3ab26faddd90..0000000000000 --- a/test/jdk/java/lang/StringCoding/ResultCachedGCTest.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* @test - * @bug 8258956 - * @summary Check if resultCached thread local is properly gc'ed. - * @run main/othervm -Xmx256m -XX:SoftRefLRUPolicyMSPerMB=0 -verbose:gc ResultCachedGCTest - */ - -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.locks.ReentrantLock; - -public class ResultCachedGCTest { - private static final int NUM_THREADS = 100; - private static final int BA_LENGTH = 100_000_000; - private static final byte[] ba = new byte[BA_LENGTH]; - private static final List workers = new ArrayList<>((int)(NUM_THREADS / 0.75f) + 1); - private static final ReentrantLock rl = new ReentrantLock(); - private static final CountDownLatch doneSignal = new CountDownLatch(NUM_THREADS); - - public static void main(String[] args) throws Exception { - for (int i = 0; i < NUM_THREADS; i++) { - var w = new Worker(); - workers.add(w); - var t = new Thread(w); - t.setDaemon(true); - t.start(); - } - - doneSignal.await(); - } - - static class Worker implements Runnable { - @Override - public void run() { - rl.lock(); // one 'new String()' at a time. - try { - System.out.println(Thread.currentThread()); - new String(ba, 0, BA_LENGTH, StandardCharsets.UTF_8); - } catch (OutOfMemoryError oome) { - throw new RuntimeException("StringCoding.resultCached was not properly GC'ed.", oome); - } finally { - rl.unlock(); - doneSignal.countDown(); - } - // keep it alive - while (true) { - try { - new CountDownLatch(1).await(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - } - } -}