Path: blob/master/test/hotspot/jtreg/gc/epsilon/TestClasses.java
40942 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 -Xmx1g -XX:MetaspaceSize=1m -XX:MaxMetaspaceSize=64m -XX:+UnlockExperimentalVMOptions -XX:+UseEpsilonGC -Xlog:gc -Xlog:gc+metaspace gc.epsilon.TestClasses34*/3536import jdk.internal.org.objectweb.asm.ClassWriter;37import jdk.internal.org.objectweb.asm.Opcodes;3839import java.util.*;40import java.io.*;41import java.nio.*;42import java.nio.file.*;4344public class TestClasses {4546static final int CLASSES = 1024;47static final int FIELDS = 1024;4849static volatile Object sink;5051static class MyClassLoader extends ClassLoader {52static final String[] FIELD_NAMES;53static {54FIELD_NAMES = new String[FIELDS];55for (int c = 0; c < FIELDS; c++) {56FIELD_NAMES[c] = "f" + c;57}58}5960public byte[] createClass(String name) {61ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);62cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC | Opcodes.ACC_SUPER, name, null, "java/lang/Object", null);63for (String fName : FIELD_NAMES) {64cw.visitField(Opcodes.ACC_STATIC | Opcodes.ACC_PRIVATE, fName, "J", null, null);65}66return cw.toByteArray();67}6869public Class<?> loadClass(String name) throws ClassNotFoundException {70if (!name.startsWith("Dummy")) {71return super.loadClass(name);72}73byte[] cls = createClass(name);74return defineClass(name, cls, 0, cls.length, null);75}76}7778public static void main(String[] args) throws Exception {79ClassLoader cl = new MyClassLoader();80for (int c = 0; c < CLASSES; c++) {81Class<?> clazz = Class.forName("Dummy" + c, true, cl);82if (clazz.getClassLoader() != cl) {83throw new IllegalStateException("Should have loaded by target loader");84}85sink = c;86}87}88}899091