Last active
September 27, 2017 04:54
-
-
Save SerCeMan/876fcdfaca602af2df8d to your computer and use it in GitHub Desktop.
GcPermgenTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.serce.gctest; | |
import java.lang.reflect.Method; | |
import javassist.ClassPool; | |
import javassist.CtClass; | |
import javassist.CtMethod; | |
public class GcPermgenTest { | |
static { | |
new OutOfMemoryError().printStackTrace(); | |
} | |
public static void main(String[] args) throws InterruptedException { | |
for(long i = 0;; i++) { | |
genClass(i); | |
} | |
} | |
private static Object genClass(long i) { | |
try { | |
ClassLoader loader = new ClassLoader() { | |
}; | |
ClassPool pool = new ClassPool(null); | |
pool.appendSystemPath(); | |
CtClass cc = pool.makeClass("Main" + i); | |
CtMethod method = CtMethod.make( | |
"public static long doSome() {" + | |
"return " + i + "L;" + | |
"}", cc); | |
cc.addMethod(method); | |
Class<?> clazz = cc.toClass(loader); | |
Method m = clazz.getMethod("doSome"); | |
return m.invoke(null, null); | |
} catch (Throwable e) { | |
e.printStackTrace(); | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment