Skip to content

Commit

Permalink
[core] Call full gc to avoid meta space oom while generating classes
Browse files Browse the repository at this point in the history
  • Loading branch information
仟弋 committed Nov 7, 2024
1 parent 048882a commit 015ac71
Showing 1 changed file with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,7 @@ private static <T> T generate(

try {
Pair<Class<?>, Object[]> result =
COMPILED_CLASS_CACHE.get(
classKey,
() -> {
GeneratedClass<T> generatedClass = supplier.get();
return Pair.of(
generatedClass.compile(CodeGenUtils.class.getClassLoader()),
generatedClass.getReferences());
});
COMPILED_CLASS_CACHE.get(classKey, () -> generateClass(supplier));

//noinspection unchecked
return (T) GeneratedClass.newInstance(result.getLeft(), result.getRight());
Expand All @@ -124,6 +117,32 @@ private static <T> T generate(
}
}

private static <T> Pair<Class<?>, Object[]> generateClass(
Supplier<GeneratedClass<T>> supplier) {
long time = System.currentTimeMillis();
RuntimeException ex;

do {
try {
GeneratedClass<T> generatedClass = supplier.get();
return Pair.of(
generatedClass.compile(CodeGenUtils.class.getClassLoader()),
generatedClass.getReferences());
} catch (OutOfMemoryError error) {
// try to gc meta space
System.gc();
try {
Thread.sleep(5_000);
} catch (InterruptedException e) {
Thread.interrupted();
throw new RuntimeException("Sleep interrupted", error);
}
ex = new RuntimeException("Meet meta space oom while generating class.", error);
}
} while ((System.currentTimeMillis() - time) < 60_000);
throw ex;
}

private static class ClassKey {

private final Class<?> classType;
Expand Down

0 comments on commit 015ac71

Please sign in to comment.