1 /*
   2  * Copyright (c) 2013, 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 /**
  25  * @test
  26  * @library /test/lib classes
  27  * @build test.Empty
  28  * @run driver ClassFileInstaller test.Empty
  29  * @run main/othervm/timeout=200 FragmentMetaspaceSimple
  30  */
  31 
  32 import java.io.DataInputStream;
  33 import java.io.File;
  34 import java.io.FileInputStream;
  35 import java.io.IOException;
  36 import java.util.ArrayList;
  37 
  38 /**
  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 space
  41  * used to store classes and meta data. Since the memory is probably allocated in
  42  * chunks per class loader this will cause a lot of fragmentation if not handled
  43  * properly since every other chunk will be unused.
  44  */
  45 public class FragmentMetaspaceSimple {
  46     public static void main(String... args) {
  47         runSimple(Long.valueOf(System.getProperty("time", "80000")));
  48         System.gc();
  49     }
  50 
  51     private static void runSimple(long time) {
  52         long startTime = System.currentTimeMillis();
  53         ArrayList<ClassLoader> cls = new ArrayList<>();
  54         char sep = File.separatorChar;
  55         String fileName = "test" + sep + "Empty.class";
  56         File file = new File(fileName);
  57         byte buff[] = read(file);
  58 
  59         int i = 0;
  60         for (i = 0; System.currentTimeMillis() < startTime + time; ++i) {
  61             ClassLoader ldr = new MyClassLoader(buff);
  62             if (i % 1000 == 0) {
  63                 cls.clear();
  64             }
  65             // only keep every other class loader alive
  66             if (i % 2 == 1) {
  67                 cls.add(ldr);
  68             }
  69             Class<?> c = null;
  70             try {
  71                 c = ldr.loadClass("test.Empty");
  72                 c.getClass().getClassLoader(); // make sure we have a valid class.
  73             } catch (ClassNotFoundException ex) {
  74                 System.out.println("i=" + i + ", len" + buff.length);
  75                 throw new RuntimeException(ex);
  76             }
  77             c = null;
  78         }
  79         cls = null;
  80         System.out.println("Finished " + i + " iterations in " +
  81                            (System.currentTimeMillis() - startTime) + " ms");
  82     }
  83 
  84     private static byte[] read(File file) {
  85         byte buff[] = new byte[(int)(file.length())];
  86         try {
  87             DataInputStream din = new DataInputStream(new FileInputStream(file));
  88             din.readFully(buff);
  89             din.close();
  90         } catch (IOException ex) {
  91             throw new RuntimeException(ex);
  92         }
  93         return buff;
  94     }
  95 
  96     static class MyClassLoader extends ClassLoader {
  97         byte buff[];
  98         MyClassLoader(byte buff[]) {
  99             this.buff = buff;
 100         }
 101 
 102         public Class<?> loadClass() throws ClassNotFoundException {
 103             String name = "test.Empty";
 104             try {
 105                 return defineClass(name, buff, 0, buff.length);
 106             } catch (Throwable e) {
 107                 throw new ClassNotFoundException(name, e);
 108             }
 109         }
 110     }
 111 }