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:
parent
9b1474b512
commit
b959e09360
@ -23,12 +23,15 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @library /runtime/testlibrary
|
|
||||||
* @library classes
|
* @library classes
|
||||||
* @build test.Empty ClassUnloadCommon
|
* @build test.Empty
|
||||||
* @run main/othervm/timeout=200 FragmentMetaspaceSimple
|
* @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;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -47,8 +50,14 @@ public class FragmentMetaspaceSimple {
|
|||||||
private static void runSimple(long time) {
|
private static void runSimple(long time) {
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
ArrayList<ClassLoader> cls = new ArrayList<>();
|
ArrayList<ClassLoader> cls = new ArrayList<>();
|
||||||
for (int i = 0; System.currentTimeMillis() < startTime + time; ++i) {
|
char sep = File.separatorChar;
|
||||||
ClassLoader ldr = ClassUnloadCommon.newClassLoader();
|
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) {
|
if (i % 1000 == 0) {
|
||||||
cls.clear();
|
cls.clear();
|
||||||
}
|
}
|
||||||
@ -59,11 +68,43 @@ public class FragmentMetaspaceSimple {
|
|||||||
Class<?> c = null;
|
Class<?> c = null;
|
||||||
try {
|
try {
|
||||||
c = ldr.loadClass("test.Empty");
|
c = ldr.loadClass("test.Empty");
|
||||||
|
c.getClass().getClassLoader(); // make sure we have a valid class.
|
||||||
} catch (ClassNotFoundException ex) {
|
} catch (ClassNotFoundException ex) {
|
||||||
|
System.out.println("i=" + i + ", len" + buff.length);
|
||||||
throw new RuntimeException(ex);
|
throw new RuntimeException(ex);
|
||||||
}
|
}
|
||||||
c = null;
|
c = null;
|
||||||
}
|
}
|
||||||
cls = 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user