java 长度不限数组,java – 如何模拟Out of memory:请求的数组大小超过VM限制

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.

我试图模仿这个

import java.util.ArrayList;

import java.util.List;

public class JavaTest {

public static void main(String[] args){

long[] ll = new long[64*1024*1024];

}

}

在我的机器上

javac *.java;java -Xmx256m JavaTest

但上面的线正在生产

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

at JavaTest.main(JavaTest.java:7)

我错过了什么?

更新:

我的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)

解决方法:

为了我

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

产生线程“main”中的异常java.lang.OutOfMemoryError:请求的数组大小超过VM限制

PS:如果你需要为了测试而产生异常,你也可以自己抛出新的OutOfMemoryError(“请求的数组大小超过VM限制”).

标签:java,jvm,out-of-memory

来源: https://codeday.me/bug/20191001/1838694.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值