How to make OutOfMemoryError on Linux JVM 64bit

in my unit test, I'm intentionally trying to raise an OutOfMemoryError exception. I use a simple operator as follows:

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

The code runs on Win7 64bit with jdk6u21 64bit. But when I run this on Centos 5 64bit with jdk6u21, you selected OutOfMemoryError, even if the size of the array is larger.

Any idea?

+3
source share
7 answers

If you just want to use 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...
    }
+3
source

Linux , , , . overcommit ( , , OOM killer ).

unittest OutOfMemoryError .

+5

128 * 1024 * 1024 * 1024 = 0, int 32-. Java 4 .

+3
ulimit -v 102400 
ulimit -d 102400
unitTest.sh

unit test 1M 1M. , ENOMEM. , /, ; .

man 2 setrlimit , . help ulimit ulimit.

+1

JVM , -Xmx.

:


public final class Test {

  public static void main(final String[] args) {
    final byte[] block = new byte[Integer.MAX_VALUE];
  }

}

JVM: -Xmx8m

:


Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Test.main(Test.java:4)
+1

, long [Integer.MAX_VALUE] 8 . (~ 16 )

+1

OutofMemoryError , .

4K , .

0

Source: https://habr.com/ru/post/1754923/


All Articles