1/* 2 * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24// ClassFileInstaller is needed to place test.Empty into well-known place 25/** 26 * @test 27 * @library /test/lib classes 28 * @build test.Empty 29 * @run driver ClassFileInstaller test.Empty 30 * @run main/othervm/timeout=200 FragmentMetaspaceSimple 31 */ 32 33import java.io.DataInputStream; 34import java.io.File; 35import java.io.FileInputStream; 36import java.io.IOException; 37import java.util.ArrayList; 38 39/** 40 * Test that tries to fragment the native memory used by class loaders. 41 * Keeps every other class loader alive in order to fragment the memory space 42 * used to store classes and meta data. Since the memory is probably allocated in 43 * chunks per class loader this will cause a lot of fragmentation if not handled 44 * properly since every other chunk will be unused. 45 */ 46public class FragmentMetaspaceSimple { 47 public static void main(String... args) { 48 runSimple(Long.valueOf(System.getProperty("time", "80000"))); 49 System.gc(); 50 } 51 52 private static void runSimple(long time) { 53 long startTime = System.currentTimeMillis(); 54 ArrayList<ClassLoader> cls = new ArrayList<>(); 55 char sep = File.separatorChar; 56 String fileName = "test" + sep + "Empty.class"; 57 File file = new File(fileName); 58 byte buff[] = read(file); 59 60 int i = 0; 61 for (i = 0; System.currentTimeMillis() < startTime + time; ++i) { 62 ClassLoader ldr = new MyClassLoader(buff); 63 if (i % 1000 == 0) { 64 cls.clear(); 65 } 66 // only keep every other class loader alive 67 if (i % 2 == 1) { 68 cls.add(ldr); 69 } 70 Class<?> c = null; 71 try { 72 c = ldr.loadClass("test.Empty"); 73 c.getClass().getClassLoader(); // make sure we have a valid class. 74 } catch (ClassNotFoundException ex) { 75 System.out.println("i=" + i + ", len" + buff.length); 76 throw new RuntimeException(ex); 77 } 78 c = null; 79 } 80 cls = null; 81 System.out.println("Finished " + i + " iterations in " + 82 (System.currentTimeMillis() - startTime) + " ms"); 83 } 84 85 private static byte[] read(File file) { 86 byte buff[] = new byte[(int)(file.length())]; 87 try { 88 DataInputStream din = new DataInputStream(new FileInputStream(file)); 89 din.readFully(buff); 90 din.close(); 91 } catch (IOException ex) { 92 throw new RuntimeException(ex); 93 } 94 return buff; 95 } 96 97 static class MyClassLoader extends ClassLoader { 98 byte buff[]; 99 MyClassLoader(byte buff[]) { 100 this.buff = buff; 101 } 102 103 public Class<?> loadClass() throws ClassNotFoundException { 104 String name = "test.Empty"; 105 try { 106 return defineClass(name, buff, 0, buff.length); 107 } catch (Throwable e) { 108 throw new ClassNotFoundException(name, e); 109 } 110 } 111 } 112} 113