如何模拟内存不足:请求的数组大小超过 VM 限制 [英] How to simulate the Out Of memory : Requested array size exceeds VM limit

查看:26
本文介绍了如何模拟内存不足:请求的数组大小超过 VM 限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了来自 sun 网站的 Out Of Memory 帮助.它被引用的地方

<块引用>

内存不足:请求的数组大小超过了 VM 限制

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

我试图通过

来模拟这个

import java.util.ArrayList;导入 java.util.List;公共类 JavaTest {公共静态无效主要(字符串[]参数){长[] ll = 新长[64*1024*1024];}}

在我的机器上

javac *.java;java -Xmx256m JavaTest

但上面的行正在生产

<上一页>线程主"java.lang.OutOfMemoryError 中的异常:Java 堆空间在 JavaTest.main(JavaTest.java:7)

我错过了什么?

更新:我的java版本是

<上一页>$java -版本java版本1.6.0_15"Java(TM) SE 运行时环境 (build 1.6.0_15-b03)Java HotSpot(TM) 服务器虚拟机(build 14.1-b02,混合模式)

解决方案

为了我

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

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

 

PS:如果你需要产生异常来进行测试,你也可以自己throw new OutOfMemoryError("Requested array size超出VM limit").

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

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.

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

解决方案

For me

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

yields 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.

这篇关于如何模拟内存不足:请求的数组大小超过 VM 限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆