test/runtime/Metaspace/FragmentMetaspaceSimple.java

Print this page

        

*** 21,36 **** * questions. */ /** * @test - * @library /runtime/testlibrary * @library classes ! * @build test.Empty ClassUnloadCommon * @run main/othervm/timeout=200 FragmentMetaspaceSimple */ import java.util.ArrayList; /** * Test that tries to fragment the native memory used by class loaders. * Keeps every other class loader alive in order to fragment the memory space --- 21,39 ---- * questions. */ /** * @test * @library classes ! * @build test.Empty * @run main/othervm/timeout=200 FragmentMetaspaceSimple */ + import java.io.DataInputStream; + import java.io.File; + import java.io.FileInputStream; + import java.io.IOException; import java.util.ArrayList; /** * Test that tries to fragment the native memory used by class loaders. * Keeps every other class loader alive in order to fragment the memory space
*** 45,69 **** } private static void runSimple(long time) { long startTime = System.currentTimeMillis(); ArrayList<ClassLoader> cls = new ArrayList<>(); ! for (int i = 0; System.currentTimeMillis() < startTime + time; ++i) { ! ClassLoader ldr = ClassUnloadCommon.newClassLoader(); if (i % 1000 == 0) { cls.clear(); } // only keep every other class loader alive if (i % 2 == 1) { cls.add(ldr); } Class<?> c = null; try { c = ldr.loadClass("test.Empty"); } catch (ClassNotFoundException ex) { throw new RuntimeException(ex); } c = null; } cls = null; } } --- 48,110 ---- } private static void runSimple(long time) { long startTime = System.currentTimeMillis(); ArrayList<ClassLoader> cls = new ArrayList<>(); ! char sep = File.separatorChar; ! String fileName = "classes" + sep + "test" + sep + "Empty.class"; ! File file = new File(System.getProperty("test.classes",".") + sep + fileName); ! byte buff[] = read(file); ! ! int i = 0; ! for (i = 0; System.currentTimeMillis() < startTime + time; ++i) { ! ClassLoader ldr = new MyClassLoader(buff); if (i % 1000 == 0) { cls.clear(); } // only keep every other class loader alive if (i % 2 == 1) { cls.add(ldr); } Class<?> c = null; try { c = ldr.loadClass("test.Empty"); + c.getClass().getClassLoader(); // make sure we have a valid class. } catch (ClassNotFoundException ex) { + System.out.println("i=" + i + ", len" + buff.length); throw new RuntimeException(ex); } c = null; } cls = null; + System.out.println("Finished " + i + " iterations in " + + (System.currentTimeMillis() - startTime) + " ms"); + } + + private static byte[] read(File file) { + byte buff[] = new byte[(int)(file.length())]; + try { + DataInputStream din = new DataInputStream(new FileInputStream(file)); + din.readFully(buff); + din.close(); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + return buff; + } + + static class MyClassLoader extends ClassLoader { + byte buff[]; + MyClassLoader(byte buff[]) { + this.buff = buff; + } + + public Class<?> loadClass() throws ClassNotFoundException { + String name = "test.Empty"; + try { + return defineClass(name, buff, 0, buff.length); + } catch (Throwable e) { + throw new ClassNotFoundException(name, e); + } + } } }