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