Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/shenandoah/oom/TestClassLoaderLeak.java
32285 views
/*1* Copyright (c) 2018, Red Hat, Inc. All rights reserved.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation.6*7* This code is distributed in the hope that it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License10* version 2 for more details (a copy is included in the LICENSE file that11* accompanied this code).12*13* You should have received a copy of the GNU General Public License version14* 2 along with this work; if not, write to the Free Software Foundation,15* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.16*17* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA18* or visit www.oracle.com if you need additional information or have any19* questions.20*21*/2223/**24* @test TestClassLoaderLeak25* @summary Test OOME in due to classloader leak26* @key gc27* @library /testlibrary28*29* @run main/timeout=480 TestClassLoaderLeak30*/3132import java.util.*;33import java.io.*;34import java.nio.*;35import java.nio.file.*;36import com.oracle.java.testlibrary.*;3738public class TestClassLoaderLeak {3940static final int SIZE = 1 * 1024 * 1024;41static final int COUNT = 128;4243static volatile Object sink;4445static class Dummy {46static final int[] PAYLOAD = new int[SIZE];47}4849static class MyClassLoader extends ClassLoader {50final String path;5152MyClassLoader(String path) {53this.path = path;54}5556public Class<?> loadClass(String name) throws ClassNotFoundException {57try {58File f = new File(path, name + ".class");59if (!f.exists()) {60return super.loadClass(name);61}6263Path path = Paths.get(f.getAbsolutePath());64byte[] cls = Files.readAllBytes(path);65return defineClass(name, cls, 0, cls.length, null);66} catch (IOException e) {67throw new ClassNotFoundException(name);68}69}70}7172static void load(String path) throws Exception {73ClassLoader cl = new MyClassLoader(path);74Class<Dummy> c = (Class<Dummy>) Class.forName("TestClassLoaderLeak$Dummy", true, cl);75if (c.getClassLoader() != cl) {76throw new IllegalStateException("Should have loaded by target loader");77}78sink = c;79}8081public static void passWith(String... args) throws Exception {82testWith(true, args);83}8485public static void failWith(String... args) throws Exception {86testWith(false, args);87}8889public static void testWith(boolean shouldPass, String... args) throws Exception {90List<String> pbArgs = new ArrayList<>();91pbArgs.add("-Xmx128m");92pbArgs.add("-XX:+UnlockExperimentalVMOptions");93pbArgs.add("-XX:+UnlockDiagnosticVMOptions");94pbArgs.add("-XX:+UseShenandoahGC");95pbArgs.addAll(Arrays.asList(args));96pbArgs.add(TestClassLoaderLeak.class.getName());97pbArgs.add("test");9899ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(pbArgs.toArray(new String[0]));100101OutputAnalyzer analyzer = new OutputAnalyzer(pb.start());102103if (shouldPass) {104analyzer.shouldHaveExitValue(0);105analyzer.shouldNotContain("java.lang.OutOfMemoryError");106analyzer.shouldContain("All good");107} else {108analyzer.shouldHaveExitValue(1);109analyzer.shouldContain("java.lang.OutOfMemoryError");110analyzer.shouldNotContain("All good");111}112}113114public static void main(String[] args) throws Exception {115if (args.length > 0) {116String classDir = TestClassLoaderLeak.class.getProtectionDomain().getCodeSource().getLocation().getPath();117for (int c = 0; c < COUNT; c++) {118load(classDir);119}120System.out.println("All good");121return;122}123124String[][][] modeHeuristics = new String[][][] {125{{"satb"}, {"adaptive", "compact", "static", "aggressive"}},126{{"iu"}, {"adaptive", "aggressive"}},127{{"passive"}, {"passive"}}128};129130for (String[][] mh : modeHeuristics) {131String mode = mh[0][0];132String[] heuristics = mh[1];133for (String h : heuristics) {134// Forceful enabling should work135passWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloading");136passWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloadingWithConcurrentMark");137138// Even when concurrent unloading is disabled, Full GC has to recover139passWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloading", "-XX:-ClassUnloadingWithConcurrentMark");140passWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloading", "-XX:-ClassUnloadingWithConcurrentMark", "-XX:ShenandoahUnloadClassesFrequency=0");141passWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:+ClassUnloading", "-XX:+ClassUnloadingWithConcurrentMark", "-XX:ShenandoahUnloadClassesFrequency=0");142143// Should OOME when unloading forcefully disabled, even if local flags try to enable it back144failWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:-ClassUnloading");145failWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:-ClassUnloading", "-XX:+ClassUnloadingWithConcurrentMark");146failWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:-ClassUnloading", "-XX:+ClassUnloadingWithConcurrentMark", "-XX:ShenandoahUnloadClassesFrequency=1");147failWith("-XX:ShenandoahGCMode=" + mode, "-XX:ShenandoahGCHeuristics=" + h, "-XX:-ClassUnloading", "-XX:-ClassUnloadingWithConcurrentMark", "-XX:ShenandoahUnloadClassesFrequency=1");148}149}150}151}152153154