问题

我知道每个对象都需要堆内存,堆栈上的每个原语/引用都需要堆栈内存。

当我尝试在堆上创建一个对象并且没有足够的内存来执行此操作时,JVM会在堆中创建一个java.lang.OutOfMemoryError并将其抛给我。

所以隐含地说,这意味着JVM在启动时保留了一些内存。

当这个保留的内存用完时会发生什么(它肯定会用完,下面会讨论),而且JVM上没有足够的内存来创建一个java.lang.OutOfMemoryError的实例?

它只是挂起来吗?或者他会抛弃我anull,因为没有记忆到new的OOM实例?

try {
    Object o = new Object();
    // and operations which require memory (well.. that's like everything)
} catch (java.lang.OutOfMemoryError e) {
    // JVM had insufficient memory to create an instance of java.lang.OutOfMemoryError to throw to us
    // what next? hangs here, stuck forever?
    // or would the machine decide to throw us a "null" ? (since it doesn't have memory to throw us anything more useful than a null)
    e.printStackTrace(); // e.printStackTrace() requires memory too.. =X
}

==
为什么JVM无法保留足够的内存?
无论保留多少内存,如果JVM无法"回收"该内存,该内存仍有可能用完:

try {
    Object o = new Object();
} catch (java.lang.OutOfMemoryError e) {
    // JVM had 100 units of "spare memory". 1 is used to create this OOM.
    try {
        e.printStackTrace();
    } catch (java.lang.OutOfMemoryError e2) {
        // JVM had 99 units of "spare memory". 1 is used to create this OOM.
        try {
            e.printStackTrace();
        } catch (java.lang.OutOfMemoryError e3) {
            // JVM had 98 units of "spare memory". 1 is used to create this OOM.
            try {
                e.printStackTrace();
            } catch (java.lang.OutOfMemoryError e4) {
                // JVM had 97 units of "spare memory". 1 is used to create this OOM.
                try {
                    e.printStackTrace();
                } catch (java.lang.OutOfMemoryError e5) {
                    // JVM had 96 units of "spare memory". 1 is used to create this OOM.
                    try {
                        e.printStackTrace();
                    } catch (java.lang.OutOfMemoryError e6) {
                        // JVM had 95 units of "spare memory". 1 is used to create this OOM.
                        e.printStackTrace();
                        //........the JVM can't have infinite reserved memory, he's going to run out in the end
                    }
                }
            }
        }
    }
}

或者更简洁:

private void OnOOM(java.lang.OutOfMemoryError e) {
    try {
        e.printStackTrace();
    } catch (java.lang.OutOfMemoryError e2) {
        OnOOM(e2);
    }
}

#1 热门回答(144 赞)

JVM永远不会耗尽内存。它预先进行堆栈的内存计算。

Structure of the JVM, Chapter 3,第3.5.2节规定:

如果可以动态扩展Java虚拟机堆栈,并且尝试进行扩展,但是可以使内存不足以实现扩展,或者如果可用的内存不足以为新线程创建初始Java虚拟机堆栈,则Java虚拟机抛出OutOfMemoryError。

ForHeap,第3.5.3节。

如果计算需要的堆数超过自动存储管理系统可用的堆,则Java虚拟机会抛出OutOfMemoryError。

因此,它在分配对象之前预先进行计算。

会发生什么是JVM尝试为内存中的对象分配内存,称为永久生成区域(或PermSpace)。如果分配失败(即使在JVM调用垃圾收集器尝试和分配可用空间之后),它也会抛出一个OutOfMemoryError。即使异常也需要内存空间,因此错误将无限期地抛出。

Further reading.?此外,OutOfMemoryError可以出现在不同的JVM structure.


#2 热门回答(64 赞)

Graham Borland seems to be right:至少myJVM显然重用了OutOfMemoryErrors。为了测试这个,我写了一个简单的测试程序:

class OOMTest {
    private static void test (OutOfMemoryError o) {
        try {
            for (int n = 1; true; n += n) {
                int[] foo = new int[n];
            }
        } catch (OutOfMemoryError e) {
            if (e == o)
                System.out.println("Got the same OutOfMemoryError twice: " + e);
            else test(e);
        }
    }
    public static void main (String[] args) {
        test(null);
    }
}

运行它会产生以下输出:

$ javac OOMTest.java && java -Xmx10m OOMTest 
Got the same OutOfMemoryError twice: java.lang.OutOfMemoryError: Java heap space

顺便说一句,我正在运行的JVM(在Ubuntu 10.04上)是这样的:

$ java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)

**编辑:**我试图看看如果使用以下程序强制JVM完全耗尽内存会发生什么:

class OOMTest2 {
    private static void test (int n) {
        int[] foo;
        try {
            foo = new int[n];
            test(n * 2);
        }
        catch (OutOfMemoryError e) {
            test((n+1) / 2);
        }
    }
    public static void main (String[] args) {
        test(1);
    }
}

事实证明,它似乎永远循环。然而,奇怪的是,尝试使用Ctrl Cdoes终止程序不起作用,但只给出以下消息:
Java HotSpot(TM) 64-Bit Server VM warning: Exception java.lang.OutOfMemoryError occurred dispatching signal SIGINT to handler- the VM may need to be forcibly terminated


#3 热门回答(41 赞)

大多数运行时环境将在启动时预分配,或以其他方式保留足够的内存来处理内存不足情况。我想大多数理智的JVM实现都会这样做。


原文链接