Path: blob/master/test/hotspot/jtreg/gc/epsilon/TestClasses.java
66644 views
/*1* Copyright (c) 2019, Red Hat, Inc. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package gc.epsilon;2425/**26* @test TestClasses27* @requires vm.gc.Epsilon28* @summary Epsilon is able to allocate a lot of classes, resizing Metaspace29*30* @modules java.base/jdk.internal.org.objectweb.asm31* java.base/jdk.internal.misc32*33* @run main/othervm -Xmx256m34* -XX:MetaspaceSize=1m -XX:MaxMetaspaceSize=64m -Xlog:gc -Xlog:gc+metaspace35* -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC36* gc.epsilon.TestClasses37*/3839import jdk.internal.org.objectweb.asm.ClassWriter;40import jdk.internal.org.objectweb.asm.Opcodes;4142import java.util.*;43import java.io.*;44import java.nio.*;45import java.nio.file.*;4647public class TestClasses {4849static final int CLASSES = 1024;50static final int FIELDS = 1024;5152static volatile Object sink;5354static class MyClassLoader extends ClassLoader {55static final String[] FIELD_NAMES;56static {57FIELD_NAMES = new String[FIELDS];58for (int c = 0; c < FIELDS; c++) {59FIELD_NAMES[c] = "f" + c;60}61}6263public byte[] createClass(String name) {64ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);65cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, name, null, "java/lang/Object", null);66for (String fName : FIELD_NAMES) {67cw.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE, fName, "J", null, null);68}69return cw.toByteArray();70}7172public Class<?> loadClass(String name) throws ClassNotFoundException {73if (!name.startsWith("Dummy")) {74return super.loadClass(name);75}76byte[] cls = createClass(name);77return defineClass(name, cls, 0, cls.length, null);78}79}8081public static void main(String[] args) throws Exception {82ClassLoader cl = new MyClassLoader();83for (int c = 0; c < CLASSES; c++) {84Class<?> clazz = Class.forName("Dummy" + c, true, cl);85if (clazz.getClassLoader() != cl) {86throw new IllegalStateException("Should have loaded by target loader");87}88sink = c;89}90}91}929394