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

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

问题描述

我使用了 Sun网站的内存不足帮助.引用为

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

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

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

但是上面的代码正在产生

But the above line is producing


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

我想念什么?

更新: 我的Java版本是

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)

推荐答案

对我来说

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

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

 

PS:如果出于测试目的需要产生异常,也可以自己throw new 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天全站免登陆