8129355: [TESTBUG] runtime FragmentMetaspaceSimple.java fails with java.lang.ClassNotFoundException: test.Empty

Avoid opening files excessively

Reviewed-by: coleenp, mseledtsov
This commit is contained in:
Ioi Lam 2015-07-02 20:30:33 -07:00
parent 9b1474b512
commit b959e09360

View File

@ -23,12 +23,15 @@
/**
* @test
* @library /runtime/testlibrary
* @library classes
* @build test.Empty ClassUnloadCommon
* @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;
/**
@ -47,8 +50,14 @@ public class FragmentMetaspaceSimple {
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();
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();
}
@ -59,11 +68,43 @@ public class FragmentMetaspaceSimple {
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);
}
}
}
}