如何在Linux JVM 64位上发生OutOfMemoryError [英] How to make OutOfMemoryError occur on Linux JVM 64bit

查看:85
本文介绍了如何在Linux JVM 64位上发生OutOfMemoryError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的单元测试中,我故意尝试引发OutOfMemoryError异常。我使用如下的简单语句:

in my unit test I deliberately trying to raise an OutOfMemoryError exception. I use a simple statement like the following:

byte[] block = new byte[128 * 1024 * 1024 * 1024];

该代码适用于Win7 64bit,jdk6u21 64bit。但是当我使用jdk6u21在Centos 5 64bit上运行时,没有抛出OutOfMemoryError,即使我将数组的大小设置得更大。

The code works on Win7 64bit with jdk6u21 64bit. But when I run this on Centos 5 64bit with jdk6u21 no OutOfMemoryError thrown, even when I make the size of the array bigger.

任何想法?

推荐答案

如果您只想消耗所有内存,请执行以下操作:

If you just want to consume all the memory do the following:

    try {
        List<Object> tempList = new ArrayList<Object>();
        while (true) {
            tempList.add(new byte[128 * 1024 * 1024 * 1024]);
        }
    } catch (OutOfMemoryError OME) {
       // OK, Garbage Collector will have run now...
    }

这篇关于如何在Linux JVM 64位上发生OutOfMemoryError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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