java 如何模拟内存不足:请求的数组大小超过 VM 限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1880687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:21:04  来源:igfitidea点击:

How to simulate the Out Of memory : Requested array size exceeds VM limit

javajvmout-of-memory

提问by DKSRathore

I used the Out Of Memory help from sun's site. Where it is quoted as

我使用了来自 sun's siteOut Of Memory 帮助。它被引用为

Out Of Memory : Requested array size exceeds VM limit

This indicates that the application attempted to allocate an array that is larger than the heap size. For example, if an application tries to allocate an array of 512MB but the maximum heap size is 256MB, then this error will be thrown. In most cases the problem is likely to be either that the heap size is too small or that a bug results in the application attempting to create an array whose size is calculated to be incorrectly huge.

内存不足:请求的阵列大小超过 VM 限制

这表明应用程序试图分配一个大于堆大小的数组。例如,如果应用程序尝试分配 512MB 的数组,但最大堆大小为 256MB,则将抛出此错误。在大多数情况下,问题很可能是堆大小太小,或者错误导致应用程序尝试创建一个大小被计算为不正确的数组。

I tried to simulate this by

我试图通过

import java.util.ArrayList;
import java.util.List;

public class JavaTest {
    public static void main(String[] args){
        long[] ll = new long[64*1024*1024];
    }
}

on my machine with

在我的机器上

javac *.java;java -Xmx256m JavaTest

But the above line is producing

但上面的线路正在生产

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at JavaTest.main(JavaTest.java:7)

What am I missing?

我错过了什么?

Update :My java version is

更新:我的 java 版本是

$java -version
java version "1.6.0_15"
Java(TM) SE Runtime Environment (build 1.6.0_15-b03)
Java HotSpot(TM) Server VM (build 14.1-b02, mixed mode)

回答by akuhn

For me

给我

long[] l = new long[Integer.MAX_VALUE];

yields Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

产量 Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit

 

 

PS: if you need to produce the exception for the purpose of testing, you could also just throw new OutOfMemoryError("Requested array size exceeds VM limit")yourself.

PS:如果你需要为了测试的目的而产生异常,你也可以只是throw new OutOfMemoryError("Requested array size exceeds VM limit")你自己。

回答by whatnick

The array is allocated as continous heap space and memory can get fragmented due to object allocaion and garbage collection. The out of memory error occurs due to lack of a solid block of memory to allocate the array. Did you sent the JVM memory explicitly with -Xmx flag ?

数组被分配为连续的堆空间,由于对象分配和垃圾收集,内存可能会碎片化。由于缺少用于分配数组的可靠内存块,会发生内存不足错误。您是否使用 -Xmx 标志显式发送了 JVM 内存?

回答by lorenzog

Try allocating, say, an ArrayList and to double its size run-time.

尝试分配一个 ArrayList 并将其大小加倍运行时。

回答by Gordon

Just looking at example fo people have the problem it seems to occur when people are using the java native interface, bridge between C++ applications.

仅看示例,人们就会遇到问题,当人们使用 Java 本机接口(C++ 应用程序之间的桥梁)时,似乎会出现这种问题。

When running pure java application the standard procedure seems for allocating an array is calculate the bytes needed from the size variable passed in the array deceleration throwing an java.lang.OutOfMemoryError: Java heap spaceexception.

运行纯 Java 应用程序时,分配数组的标准程序似乎是从数组减速中传递的大小变量中计算所需的字节数,从而引发java.lang.OutOfMemoryError: Java heap space异常。

When the array is created outside the JVM it looks it generates this other exception. I'm sure there is some technical reason for this different behavior.

当数组在 JVM 之外创建时,它看起来会生成另一个异常。我确信这种不同的行为有一些技术原因。

You could try replicating the problem this person describes on this other forum.

您可以尝试复制此人在其他论坛上描述的问题。