Path: blob/master/test/hotspot/jtreg/runtime/Metaspace/FragmentMetaspaceSimple.java
40942 views
/*1* Copyright (c) 2013, 2021, Oracle and/or its affiliates. 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*/2223// ClassFileInstaller is needed to place test.Empty into well-known place24/**25* @test26* @library /test/lib classes27* @build test.Empty28* @run driver jdk.test.lib.helpers.ClassFileInstaller test.Empty29* @run main/othervm/timeout=200 FragmentMetaspaceSimple30*/3132import java.io.DataInputStream;33import java.io.File;34import java.io.FileInputStream;35import java.io.IOException;36import java.util.ArrayList;3738/**39* Test that tries to fragment the native memory used by class loaders.40* Keeps every other class loader alive in order to fragment the memory space41* used to store classes and meta data. Since the memory is probably allocated in42* chunks per class loader this will cause a lot of fragmentation if not handled43* properly since every other chunk will be unused.44*/45public class FragmentMetaspaceSimple {46public static void main(String... args) {47runSimple(Long.valueOf(System.getProperty("time", "80000")));48System.gc();49}5051private static void runSimple(long time) {52long startTime = System.currentTimeMillis();53ArrayList<ClassLoader> cls = new ArrayList<>();54char sep = File.separatorChar;55String fileName = "test" + sep + "Empty.class";56File file = new File(fileName);57byte buff[] = read(file);5859int i = 0;60for (i = 0; System.currentTimeMillis() < startTime + time; ++i) {61ClassLoader ldr = new MyClassLoader(buff);62if (i % 1000 == 0) {63cls.clear();64}65// only keep every other class loader alive66if (i % 2 == 1) {67cls.add(ldr);68}69Class<?> c = null;70try {71c = ldr.loadClass("test.Empty");72c.getClass().getClassLoader(); // make sure we have a valid class.73} catch (ClassNotFoundException ex) {74System.out.println("i=" + i + ", len" + buff.length);75throw new RuntimeException(ex);76}77c = null;78}79cls = null;80System.out.println("Finished " + i + " iterations in " +81(System.currentTimeMillis() - startTime) + " ms");82}8384private static byte[] read(File file) {85byte buff[] = new byte[(int)(file.length())];86try {87DataInputStream din = new DataInputStream(new FileInputStream(file));88din.readFully(buff);89din.close();90} catch (IOException ex) {91throw new RuntimeException(ex);92}93return buff;94}9596static class MyClassLoader extends ClassLoader {97byte buff[];98MyClassLoader(byte buff[]) {99this.buff = buff;100}101102public Class<?> loadClass() throws ClassNotFoundException {103String name = "test.Empty";104try {105return defineClass(name, buff, 0, buff.length);106} catch (Throwable e) {107throw new ClassNotFoundException(name, e);108}109}110}111}112113114