Solve OutOfMemoryError UncaughtExceptionHandler 2016-07-08 07:40

In this page I will show you how to solve Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread. This error means there is not enough memory to run your application. What you need to do is increasing memory. The key question is there are a lot of arguments for java which is right?

short answer

Add arguments like following. The value of PermSize you can tune by your own environment.

-XX:PermSize=60M -XX:MaxPermSize=512M

long answer

You've seen this error before: java.lang.OutOfMemoryError: PermGen space and you know how to solve it. Just increase the value of PermSize. I write a simple example to help me understand this error.

 * -XX:PermSize=10M -XX:MaxPermSize=10M
public class JavaMethodAreaOOM {
    static class OOMObject {


    public static void main(String[] args) {
        while (true) {
            Enhancer enhancer = new Enhancer();
            enhancer.setCallback(new MethodInterceptor() {
                public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                    return methodProxy.invoke(o, objects);

Use cglib to generate class you need add the dependence, the pom file like following.


Add the arguments -XX:PermSize=10M -XX:MaxPermSize=10M before run this example. I run this example in different jdk and get different result.

  • jdk6
    Exception in thread "main" net.sf.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
        at net.sf.cglib.core.AbstractClassGenerator.create(
        at net.sf.cglib.proxy.Enhancer.createHelper(
        at net.sf.cglib.proxy.Enhancer.create(
        at com.henryxi.jvm.JavaMethodAreaOOM.main(
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(
        at java.lang.reflect.Method.invoke(
        at com.intellij.rt.execution.application.AppMain.main(
    Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(
        at java.lang.reflect.Method.invoke(
        at net.sf.cglib.core.ReflectUtils.defineClass(
        at net.sf.cglib.core.AbstractClassGenerator.create(
        ... 8 more
    Caused by: java.lang.OutOfMemoryError: PermGen space
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(
        at java.lang.ClassLoader.defineClass(
        ... 13 more
  • jdk7
    Exception in thread "main" 
    Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
  • jdk8 There no exception thrown.

After reading this blog About G1 Garbage Collector, Permanent Generation and Metaspace I understand. PermGen supported in jdk6 and jdk7 but not supported in jdk8. But in jdk7 when PermGen is not enough it won't throw "OutOfMemoryError: PermGen space" it will throw "OutOfMemoryError thrown from the UncaughtExceptionHandler". Hotspot is giving up "Permanent Generation" step by step it use "Metaspace" instead. You can learn the detail in above link.